🚀 Supercharge your YouTube channel's growth with AI.
Try YTGrowAI FreePython Class Attribute and Instance Attribute

In this article, we will be focusing on Python Class Attribute and Instance Attribute.
Attributes are the key-players of a programming language. They are responsible for holding important data values and also help in data manipulation.
Let us now get started!
Understanding Python Class Attribute
Python Class Attribute is an attribute/variable that is enclosed within a Class. That is, its scope lies within the Python class.
The Class attribute creates only a single copy of itself and this single copy is shared and utilized by all the functions and objects within that particular class.
Syntax:
class Class-name:
variable = value
Let us now understand the implementation of the same through the below example.
Implementing the Class Attribute with an example
class class_attribute:
val = 1
def product(self):
class_attribute.val *= 10
print(class_attribute.val)
obj1 = class_attribute()
obj1.product()
obj2 = class_attribute()
obj2.product()
In this example, we create a class variable ‘val’ and initialize it to 1.
Further, we access the variable ‘val’ within the function product() and manipulate the value by multiplying it with 10.
As clearly observed, the same copy of the variable ‘val’ is used by both of the objects created. Thus at first, val = 1.