Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages
wb_sunny

Python Class Attribute and Instance Attribute

PYTHON 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.

When the object obj1 calls the function, the same copy of ‘val’ is used(the value does not get reset) and thus it becomes val=10. On being called by the obj2, val becomes val*10 i.e. 10*10 = 100.

Output:

10
100

Understanding the Python Instance Attribute

Python Instance attribute is a local attribute/variable whose scope lies within the particular function that is using the attribute. Thus, it is enclosed by a particular function.

The Instance attribute creates a new copy of itself, each time when it is being called by a function/object. That is, a distinct copy of this variable gets utilized every time the object or function tries to access it.

Syntax:

def function-name():
    variable = value

Let us now implement local attributes with the help of an example.


Implementing Instance Attribute with an example

class instance_attribute: 

	def product(self): 
	   val = 20
	   val *= 10
	   print(val)


obj1 = instance_attribute() 
obj1.product()		 
 
obj2 = instance_attribute() 
obj2.product()

In this example, we have declared and initialized an instance attribute as val = 20.

Further, when obj1 tries to access the variable through the function, it creates its own new copy as resets the default value to the initialized value and then provides access to it.

The same scenario gets repeated when obj2 tries to access the instance variable ‘val’.

Output:

200
200

Conclusion

By this, we have come to the end of this topic. Feel free to comment below, in case you come across any question.


References