How object and class attributes work in Python

An attribute is another term for a field. It can be a public, private or protected constant, variable, method that can be accessed directly. Everything in Python is an object, and almost everything has attributes and methods. In python, functions are objects too, so they have attributes like other objects. All functions have a built-in attribute __doc__, which returns the doc string defined in the function source code.

are attributes which are owned by the class itself. They will be shared by all the instances of the class. Therefore they have the same value for every instance. We define class attributes outside of all the methods, usually, they are placed at the top, right below the class header.

variable belonging to one, and only one, object. This variable is only accessible in the scope of this object and it is defined inside the constructor function, __init__(self,..) of the class.

To list the attributes, we have two functions:-
1. vars()- This function displays the attribute of an instance in the form of a dictionary.
2. dir()- This function displays more attributes than vars function, as it is not limited to instance. It displays the class attributes as well.

A python decorator lets a method to be accessed as an attribute instead of as a method. The main work of decorators is they are used to add functionality to the existing code. Also called metaprogramming, as a part of the program tries to modify another part of the program at compile time. They are needed because if you change the value of an attribute inside a class, the other attributes that are derived from the attribute you just changed don’t automatically update.

However, there are a couple of conventions you need to follow when defining a setter method:

  1. The setter method should have the same name as the equivalent method that @propertydecorates.
  2. It accepts as an argument the value that the user sets to the property.

Finally, you need to add a @{methodname}.setter decorator just before the method definition.

Once you add the @{methodname}.setter decorator to it, this method will be called every time the property is set or changed, as you can see in the following example:

In Python, all instance variables are stored as a regular dictionary. When working with attributes, you just changing a dictionary. We can access instance dictionary by calling __dict__ magic method and a class dictionary can also be accessed from an instance, using __class__ method (i.e., car.__class__.__dict__) because all methods belong to a class, they are also stored in this dictionary.

As we can see in the image at the top of this review when we call a variable Python search at first in the instance __dict__ and if the variable isn’t there, search again in the class __dict__ and if there is no result throw an error.

--

--

--

Software and Chemical Engineer

Love podcasts or audiobooks? Learn on the go with our new app.

Best FTP Client Softwares For WordPress

Smart Containers POC | RIOT + LORA + FIT/IoT-LAB + TTN + TAGO.IO + HYPERLEDGER FABRIC — Part 1

The Quick Beginners Guide to Interactive Brokers API (Python)

Essential Skills Needed By a Quality Engineer Today…

Step-by-step guide to create an auto scaled GitLab runner on AWS

A simple guide to creating a GitHub Pull Request

Points to Consider Before Making a Hybrid Mobile App

The story of wood and arrays

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Rodrigo Sierra Vargas

Rodrigo Sierra Vargas

Software and Chemical Engineer

More from Medium

Chapter Four of Eloquent JavaScript: Data structures- Object and Array.

Early Returns/Exits — Code Patterns in JavaScript

Fun with Promises in Javascript

7 Reasons to run React JS for speed boosting in software development vs Angular JS