Write a blog post describing how object and class attributes work.
A class is a custom data type, In python, we can create custom classes that are fully integrated and that can be used just like the built-in data types. For example, dict, int and str. We use the term object and instance, to refer to an instance of a particular class.
What’s a class attribute?
Before to describe this new concept, let’s clarify that Objects usually have attributes and methods that are callable attributes too, and other attributes are data. Data attributes are normally implemented as instance variables, that is, variables that are unique to a particular object. Then:
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. For example:
Own source
Previous image, we see in a interactive python session the definition of a class attribute, Besides, we see that we can access a class attribute via an instance or via the class name:
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.
Own source
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.
Own source
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:
Own source
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:
Own source
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 differences between class and instance attributes?
As we saw in the previous example, class and instance attributes are different. Class attributes are accessible to all instances of the class, where instance attributes are accessible only to that particular instance of the class. We can access a class attribute via an instance or the class name, whereas an instance attribute can only be accessed via the instance to which it belongs.
What are the advantages and drawbacks of class and instance Attributes?
Advantages of class attributes:
All instances of the class inherit them from the class.
Class attributes are often used to define constants which are closely associated with a particular class.
They store data that is relevant to all the instances. For example, we could have a counter class attribute that increments every time we create a new instance and decrements every time we delete an instance. This way we can always keep track of how many instances of the same class we created.
Using classes provides the ability to reuse the code which makes the program more efficient.
Disadvantages of class attributes:
It can get messy when you create an instance in which the value of the class attribute is different than the class attribute value, and then try to retrieve it through another instance. The behavior quickly becomes unexpected.
When we set an attribute on an instance which has the same name as a class attribute, we are overriding the class attribute with an instance attribute, which will take precedence over it.
Advantages of instance attributes:
They are specific to an object and are easy to set and get thanks to properties.
They are discarded once the instance is deleted, so they die with the instance they are associated with, which makes things clearer.
It is very common for an object’s methods to update the values of the object’s attributes
Disadvantages of instance attributes:
They don’t allow keeping track of values in between instances.
The fact that their values are lost on deletion is also a disadvantage in some cases where you want to keep a history of values for example.
How does Python deal with the object and class attributes using the __dict__
Python’s class attributes and object attributes are stored in separate dictionaries. which they use to store their attributes and their corresponding values, as we can see here:
Own source
The output will be:
Own source
Firstable, we printed the instance attributes of my_rectangle_1. They were initialized when object was instantiated. The same prints the other instance of the rectangle object. In the last line, we can see that applying __dict__ on the Rectangle class also prints a dictionary with attributes, because even if Rectangle is a class, it is technically an object too and has attributes that can be listed.