In this section, we will learn about the class attributes and Instance attributes in Python.
Note: we’re assuming you already read the section related to the class and object in Python.
What is Attribute in Python?
An attribute is a local variable that we put into a class. Attributes are used to set or get the state of an object.
For example, let’s say we have a Human class. We can put an attribute for this class with the name let’s say `running` with the values either `True` or `False`. If the value was True, that means the human object created from the `Human` class is in the state of running. But if the value was False, that means the human object is not running.
So, as you can see, using attributes, we can define states for objects and change these states if needed.
Python types of attributes:
There are two types of attribute in a class.
- Instance Attributes: Those attributes that are created in the body of the `__init__` method.
- Class Attributes: Those attributes that are created in the body of the target class itself! This second type of attribute will not be declared within the body of any method.
Python instance attributes:
Instance attributes are those type of attributes that we create within the body of the __init__ method! They are called instance attribute because every time we create a new object from a class, that object will have its own copy of the specified attributes! This means if there are two objects created from the same class, if one object changed the value of one of its attributes, the value of the same attribute for the second object won’t change! This is because each object has its own independent attribute.
Note: we can use the name of objects + the dot `.` operator to access its attributes.
Example: using instance attributes in python
class Parent: def __init__ (self, firstName, lastName): self.firstName = firstName self.lastName = lastName parent1 = Parent("Jo","Doe") parent2 = Parent("Ja","May") parent1.firstName = "John" parent2.firstName = "James" print(parent1.firstName) print(parent2.firstName)
Output:
John James
In this example, we’ve created two objects named `parent1` and `parent2` from the `Parent` class.
As you can see, we’ve accessed the `firstName` attribute of each of these objects and changed their values to something different.
At the end, we can see that changing the value of one attribute won’t affect the value of the same attribute in another object.
That proves each object has its own independent attributes that are defined within the body of the `__init__` method.
Python Class Attribute (AKA static Attribute)
On the other hand, we have attributes that are created outside of any method in a class! These are called class attribute!
The main difference between a class attribute and an instance attribute is that `class attributes` are not copied per each object’s creation!
Basically, if we create one class attribute, that will be shared between the entire objects that are created from the target class.
So if we use one object to change the value of a class attribute, then the other objects of the same class will notice this change.
Now the question is how can we access a Class attribute?
Well, in order to only retrieve the value assigned to a class attribute, we can use the name of objects just like the way we use them to access an instance attribute. Also, we can use the name of the owner class to access a class attribute as well.
But if the purpose of accessing a class attribute is to modify the content, then there’s only one option, and that is to use the name of the class. This is because if we use an object to access a class attribute, that will create a new attribute in the target object instead of modifying the content of that attribute class.
Example: creating python class attributes and accessing the attributes
class Parent: cAttribute = "This is a class attribute" def __init__ (self, firstName, lastName): self.firstName = firstName self.lastName = lastName parent1 = Parent("Jo","Doe") parent2 = Parent("Ja","May") print(parent1.cAttribute) Parent.cAttribute = "Yup, this is definitely a class attribute" print(parent2.cAttribute)
Output:
This is a class attribute Yup, this is definitely a class attribute
As you can see, we can access a class attribute using objects as well as the class name. But we only use the class name to change the value of a class attribute.
Adding Attributes to Python Objects
Other than the attributes that we create within the body of the `__init__` method, an object is capable of creating new attributes on the fly as well!
When we say on the fly, we mean a new attribute can be added to an object after that object is created.
To do this, simply call the attribute you want to create for an object, on the left side of the assignment operator `=` as if the attribute is already created and put a value to that attribute on the right side of this operator.
The execution engine then will create a new attribute with the specified value for that object.
Note that this new attribute is only for the target object! That means other objects of the same class won’t have such an attribute.
Example: adding attributes to Python Objects
class Parent: cAttribute = "This is a class attribute" def __init__ (self, firstName, lastName): self.firstName = firstName self.lastName = lastName parent1 = Parent("John","Doe") parent2 = Parent("James","May") parent1.age = 200 parent1.email = "[email protected]" print(parent1.age) print(parent1.email)
Output:
200 [email protected]