Change language

Python class and instance attribute


Attributes &these are the key players in the programming language. They are responsible for storing important data values ​​and also help in data manipulation.


Understanding the Python class attribute

Python Class Attribute &it is an attribute or variable that is enclosed in a class. That is, its scope is in the Python class.

The Class attribute creates only one copy of the itself, and this single copy is used by all functions and objects in that particular class.


 class Class-name : variable = value < / pre> 

Let’s now figure out the implementation of the same in the example below.

Implementation in the 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 () 

B In this example, we create a class variable val and initialize it to 1. Next, we access the val variable in the product () function and manipulate the value by multiplying it by 10.

As you can clearly see, the same a copy of the val variable is used by both created objects. Thus, first val = 1.

When obj1 calls a function, the same copy of val is used (the value is not cleared) and thus becomes val = 10. When obj2 is called, val becomes val * 10, i.e. 10 * 10 = 100.


 10 100 

Understanding the Python Instance Attribute

Python Instance attribute &it is a local attribute or variable whose scope is within a particular function using the attribute. Thus, it is wrapped in a specific function.

The Instance attribute creates a new copy of itself each time it is called by a function or object. That is, a separate copy of this variable is used each time an object or function tries to access it.


 def function-name (): variable = value 

Let’s now implement local attributes by example.

Implementing an instance attribute

 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 the instance attribute as val = 20.

Also, when obj1 tries to access a variable through a function, it creates its own new copy, since it resets the default to an initialized value and then grants access to it.

The same scenario is repeated when obj2 tries to access the instance variable val. < / p>


 200 200 


Best laptop for Sims 4


Best laptop for Zoom


Best laptop for Minecraft


Best laptop for engineering student


Best laptop for development


Best laptop for Cricut Maker


Best laptop for hacking


Best laptop for Machine Learning


Latest questions


psycopg2: insert multiple rows with one query

12 answers


How to convert Nonetype to int or string?

12 answers


How to specify multiple return types using type-hints

12 answers


Javascript Error: IPython is not defined in JupyterLab

12 answers



Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method