how object and class attributes work ?

Khalil Hassayoun
5 min readJan 11, 2021

What’s a class attribute ?

A class attribute is a Python variable that belongs to a class rather than a particular object. It is shared between all the objects of this class and it is defined outside the constructor function, __init__(self,…) , of the class.

What’s an instance attribute ?

An instance attribute is a Python variable belonging to only one object. It is only accessible in the scope of the object and it is defined inside the constructor function of a class.

What are the differences between class and instance attributes ?

An instance attribute is a Python variable belonging to one, and only one, object. … A class attribute is a Python variable that belongs to a class rather than a particular object. It is shared between all the objects of this class and it is defined outside the constructor function, __init__(self,…) , of the class.

What’s an instance attribute and the ways to create them?

Instance attributes are owned by the specific instances of a class. This means for two different instances the instance attributes are usually different. Next example, showns this definition:

If we have two instances and we want to change a class attribute, you have to do it with the notation ClassName.AttributeName. Otherwise, you will create a new instance variable.

What is the Pythonic way to create them ?

In the previous definition of both concepts; we define class attributes outside of all the methods, usually they are placed at the top, right below the class header. i.e.

Instance attributes by just declaring them and assigning a value to them outside of the class. There is another way to do it, which uses the __init__ method. This is the popular “constructor” method used in many OOP languages. Constructor is automatically called after an instance of a class has been created. It’s usually the first method defined inside a class. For example:

self keyword in magic method __init__ refers to the current instance, or object, of the class. Now, from outside of the class, we can create a new instance:

The __init__ method will be called and the instance attributes width and height will be set to 8, 4 respectively in my_rectangle_1 object.

What are the advantages and drawbacks of class and instance Attributes ?

Advantages of class attributes:

The class inherits all instances of the class from them. Class attributes are also used for identifying constants that are closely related to a specific class. They store information that is applicable to all situations. For instance, when we delete an instance, we might have a counter class attribute that increases every time we create a new instance and decrements. In this way, we can still keep track of how many cases we have generated in the same class. The use of classes offers the opportunity to reuse the code, making the software more efficient.

Disadvantages of class attributes:

When you create an instance where the value of the class attribute is different from the value of the class attribute, it can get messy and then try to recover it via another instance. The conduct becomes unexpected easily.

We override the class attribute with an instance attribute when we set an attribute on an instance that has the same name as a class attribute,That is going to take priority over it.

Advantages of instance attributes:

They are unique to an entity and, thanks to properties, are easy to set and get.

Once the instance is deleted, they are discarded, so they die with the instance they are connected to , That makes matters simpler.

Methods for an object to change the values of the object’s attributes are very common.

Disadvantages of instance attributes:

They don’t allow the values between instances to be monitored . In certain situations where you choose to retain a background of values, for example, the fact that their values are lost upon deletion is also a drawback.

How does Python deal with the object and class attributes using the __dict__ ?

In different dictionaries, Python’s class attributes and object attributes are stored. As we can see here, what they use to store their attributes and their corresponding values:

as a result the output would be :

first the example attributes of my rectangle 1 were printed . As the object was instantiated, they were initialized. A particular instance of the rectangle object is printed the same way. We can see in the last line that applying a dictionary with attributes to the Rectangle class also prints a dictionary, because even though Rectangle is a class, it is also technically an entity and has attributes that can be specified .

i hope my blog helped you understand more about the classes and objects attributes and how they work .

--

--

Khalil Hassayoun
0 Followers

Holberton school student . Future software engineer