Skip to main content

Class and Instance Attributes in Python

Class Attributes 

Class attributes are attributes which are owned by the class itself. It is the variables defined directly in the class that are shared by all objects of the class. It is unique to each class. Each instance of the class will have this attribute.

This attributes are defined in the class body parts usually at the top, for right below the class header. It is the variables defined directly in the class that are shared by all objects of the class.

It is a Python Variable that belongs to a class rather than a particular object. It is shared between all other objects of the same class and is defined outside the constructor function,  __init__(self,...) of the class

EXAMPLE :

Input : 

class func():
    a = 10

    def __init__(self):
        self.a= 20

res = func()
print(res.a)  
print(func.a)

Output : 

20
10

Instance Attributes

Instance attributes are attributes or properties attached to an instance of a class. Instance attributes are defined in the constructor.It is unique to each object.

It is a Python variable belonging to only one object. It is only accessible in the scope of the object and it is defined inside the constructor function of a class. 

EXAMPLE :

Input : 

class abc():
    def __init__(self, radius):
        self.pi = 3.14
        self.r = radius

    def area(self):
        return self.pi * self.r**2
c = abc(10)
print(c.pi)  

Output: 

3.14

Instance attributes are not shared by objects. Every object has its own copy of the instance attribute. we can use these two functions to list the attributes of an instance :-

  •  vars() – Display the attribute of an instance in the form of a dictionary.
  •  dir() –  Display all the attributes from class attributes also because it is not limited to instance attributes only.

NOTE : In case of class attributes all object refer to single copy.

EXAMPLE: Using dir() and var()

Input : 

class Student:
    cl_var = 20
    def __init__(self):
        self.name = 'abc'
        self.rollno = 15
  
    def func_1(self):
        print(self.name)
        print(self.rollno)
  
a = Student()

print("Attributes of an instance: ", vars(a))
print("Attributes of class: ", dir(a))

Output:

Attributes of an instance:  {'name': 'abc', 'rollno': 15}
Attributes of class:  ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'cl_var', 'func_1', 'name', 'rollno']

observe in above output cl_var is not part of instance attribute but it is part of class attribute.

Tags
Submitted by devanshi.srivastava on July 26, 2021

Devanshi, is working as a Data Scientist with iVagus. She has expertise in Python, NumPy, Pandas and other data science technologies.

About

At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.