Difference between instance and class attributes | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Difference between instance and class attributes

I'm trying to learn about the instance and class attributes in python. Then am a little confused: is this_obj.var an instance attribute or it belongs to the class attribute. The code is below class Myclass (object): var = 10 this_obj = Myclass() this_obj.somevar = 12 that_obj = Myclass() that_obj.somevar = 12

2nd Nov 2017, 8:54 AM
Peter
Peter - avatar
2 Answers
+ 3
Instance attributes are owned by the specific instances of a class. This means for two different instances the instance attributes are usually different. Class attributes are attributes which are owned by the class itself. They will be shared by all the instances of the class.
3rd Nov 2017, 9:16 AM
Talluri Saisumanth
Talluri Saisumanth - avatar
+ 2
this_obj and that_obj are instances
3rd Nov 2017, 1:41 AM
Daniel
Daniel - avatar