Introduction to Python Class Attributes
An attribute of class means these are the terms used for describing the properties or fields or data members or class members of the class. Attribute simply means the data member of an object. In Python, the class attribute is an attribute of the class which belongs to the class itself which means it is the attribute of the class rather than an attribute of an instance of a class. The Python class attributes will be shared by all the instances where instance attributes are not shared between instances. These class attributes are usually defined in the class body parts at the top for better understanding.
Working of the Class Attribute with examples
An attribute is a changeable property or any characteristics of any component or program or class which can set to different values. For example: In general, the attribute for humans can be hair, the color of the face, etc.
So let us have an example of how the class attributes are created in the below example:
count = 0
classwithattribute.count += 1
c1 = classwithattribute()
c2 = classwithattribute()
In the above example we have a class named “classwithattribute” and class attribute as “count” which is initialized to value “0” then we are writing a function to increase the value of count attribute by 1 and this is done in a function called “increment”. Then we create a variable of the class and then we call the function with the class variable “c1”.
In Python, attributes which are used to implement access controls of classes. There are several built-in methods and functions to access these attributes of the class.
- getattr(): this is used to get the attribute so that it can get to access these attributes of the object.
- hasattr(): this function is used to know whether the attributes are present in the class or not.
- setattr(): this method is used to assign the values to the attribute and this method also creates the attribute and then sets the attribute if the attributes are not present.
- delattr(): this function is used for deleting the attribute created for the class and it will give an error if trying to access the same attribute after deleting it.
Let us consider an example to demonstrate all the above built-in methods that are used to access the class attributes.
s1 = student()
print("This will display the attribute value")
print("This will check if the attribute is present or not")
print("This will set new attribute with its value")
print("This will delete the attribut specified")
print("This will give error as it is already deleted")
In the above example, we have written code to demonstrate the above-accessing methods for class attributes. In the above we have created a class “student” with attributes “name”, “age” to display these details we have a “display” function so this class variable “s1” is used in all the methods. In getattr(s1, name) we are asking to fetch the attribute value of attribute “name” as Rob which is assigned at the beginning of attribute creation. Similarly hasattr(s1, “name”) this searches for the “name” attribute in the class “student” if it exists then returns true else false. Then setattr(s1, “weight”) this function will create a new attribute if not there or else it will assign the value to the attribute. And lastly the delattr (student,’ age’) this function will delete the attribute “age” from the class “student”. This attribute will no more be accessible after deleting the attribute.
As the class attributes can be accessed from one class to another class. In this section, we will see accessing attributes and methods of the class in another class. So to access the attributes or methods of one class in any other class we have to do it bypassing the object of the class to another class. Let us consider an example to demonstrate this concept:
self.a1 = 1
self.a2 = 2
self.a1 = self.a1 + self.a2
def __init__(self, class_first):
self.a1 = class_first.a1
self.a2 = class_first.a2
object1 = firstclass()
summation = object1.methodfirst()
object2 = secondclass(object1)
In the above program, there are two classes defined “firstclass” and “secondclass”, in the “firstclass” we have defined the function of addition of two numbers which are declared as attributes in “firstclass” and then this “firstclass” is passed to “secondclass” so the objects of both classes are created and it returns the summation of two numbers.
Let us see when we can use Python class attributes:
- So we know that class attributes can be accessed within the class and also in another class, so it is often used to store some constants as we did in the above program for accessing the class attribute of one class in another class.
- Sometimes the class attributes are used to define some default values. If one class is having the attribute to hold some limit value or some bounded list so we need to create an instance by assigning some instance’s limit attribute.
In Python, many have the thinking that class attributes concept are a bit tricky so they are not used much, but we should take care of how and when to use these. So in general class attributes, class attributes are nothing but simple properties of the class which they define. There are many different methods and functions to access the class attributes as well as there is an option in which class attributes of one class can be accessed in other class rather instances can access only within the class.
This is a guide to Python Class Attributes. Here we discuss the introduction, working of the Class Attribute and the examples of attributes along with outputs. You can also go through our other suggested articles to learn more –