How object and class attributes work in Python

Rodrigo Sierra Vargas
4 min readMay 28, 2019

Attributes:

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.

Class attributes:

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.

Instance Attributes:

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.

The Pythonic way to create attributes using Decorators:

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:

What is __dict__ and how does Python deal with the object and class attributes

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.

References

--

--