Hello Ninjas! Do you know about “what is the difference between class attributes and instance attributes”? Whenever we create a class in Python, we usually create attributes. These attributes are shared across every object of a class.
In this article, we will discuss each of the attributes in brief. We will guide you with every detail you need to know about what is the difference between class attributes and instance attributes with their implementation.
Class Attributes
Class Attributes are class variables and are defined directly in class. They belong to the class itself rather than a particular object. The variables are shared by every object of the class. In class attributes, all object refer to single copy. The value remains the same for every new object in class attributes.
Let’s see the implementation of the class attribute:
In the above implementation, we created a class named Student. We created a variable Educator inside the class.
Here, the Educator variable acts as a class Variable. The variable ‘Educator’ is defined outside the constructor. In the later part of this article, we will see how name and course act as instance attributes.
Explanation
Below is the explanation for the class attribute:
class Student:
# class attributes
Educator = "CodingNinjas"
In the above code, the value of the variable Educator is ‘CodingNinjas’. For FirstStudent and SecondStudent, the educator for both of them is CodingNinjas. Thus, the variable is shared among both the students in class attributes.
Instance Attributes
An instance is another name for the object. The Instance attributes are unique to each object. The instance attributes are not shared by objects inside the class. It is unique property attached to an instance of the class. The value remains specific to objects in the class.
Let’s see the implementation of the instance attribute:
In the above implementation, we created a class named Student. We had created two variables in the constructor __init__() function, namely ‘name’ and ‘course’. We initialized them using the self parameter. The variable name and course are the instance attributes.
Explanation
Let's look at their unique output for both FirstStudent and SecondStudent:
For the FirstStudent, the name of the student and the course opted by him is unique. The variable name for the First_Student is ‘Ankit,’ and the variable course name is ‘DSA’.
For the Second_Student, the name of the student and the course opted by him is unique. The variable name for the Second_Student is ‘Vaibhav,’ and the variable course name is ‘Cpp.’
Difference between Class Attributes and Instance Attributes
In this section we will see what is the difference between class attributes and instance attributes; let's have a look:
Class Attributes
Instance Attributes
It is defined directly inside a class.
It is defined inside a constructor using self parameter.
The variables are shared by every object of the class.
The instance attributes are not shared by objects inside the class.
Defined outside the __init__() function.
Defined inside the __init__() function.
Class attributes are defined outside the constructor.
Instance attributes are defined inside the constructor.
Usually defined at the top of the class body.
Initialized below the class attribute inside the constructor.
Class attributes are accessed using class name as well as using an object with dot notation. classname.class_attribute or object.class_attribute
Instance attributes are accessed using object dot notation.
object.instance_attribute
Changes in the value of class attributes will be reflected to all the objects in the class.
Changes in the value of instance attributes will not be reflected to other objects in the class.
What are the main features of object-oriented programming?
Class, Object, Inheritance, Encapsulation, and Abstraction Polymorphism are the main features of object-oriented programming. These features provide flexibility, extensibility, reusable, and ease of understanding our code.
What is an instance variable?
An instance is another name for the object. Variables whose values are assigned inside the init method(constructor) or method with self are called instance variables.
What is __ init __ method in Python class?
In Python, __init__ is a reserved method. It is referred to as a constructor in object-oriented terminology. Whenever we create an object in a class, the __ init __ method is called. It allows the class to initialize the class attributes.
What is self keyword in Python?
The class instance is represented by self. We can access the class's attributes and methods using the "self" keyword in Python. It associates the properties with the arguments provided. As Python does not use the syntax to refer to instance attributes, we must use self.
Conclusion
This article briefly discussed what is the difference between class attributes and instance attributes. We have discussed them in brief and along with their implementation. You can check out our other blogs to enhance your knowledge: