Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Class Attributes
2.1.
Explanation
3.
Instance Attributes
3.1.
Explanation
4.
Difference between Class Attributes and Instance Attributes
5.
Frequently Asked Questions
5.1.
What are the main features of object-oriented programming?
5.2.
What is an instance variable?
5.3.
What is __ init __ method in Python class?
5.4.
What is self keyword in Python?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

What is the difference between Class Attributes and Instance Attributes

Introduction

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.

difference between Class Attributes and Instance Attributes

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:

class Student:

    # class attributes
    Educator = "CodingNinjas"
    
    def __init__(self, name, course):
        self.name = name
        self.course = course
    
FirstStudent = Student("Ankit", "DSA")
SecondStudent = Student("Vaibhav", "Cpp")

print(FirstStudent.name)
# output = Ankit
print(SecondStudent.name)
# output = Vaibhav

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:

class Student:
    
    # instance attributes
    def __init__(self, name, course):
        self.name = name
        self.course = course
    
FirstStudent = Student("Ankit", "DSA")
SecondStudent = Student("Vaibhav", "Cpp")

print(FirstStudent.name)
# output = Ankit
print(FirstStudent.course)
# output = DSA

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 FirstStudent:

FirstStudent = Student("Ankit", "DSA")

print(FirstStudent.name)
# output = Ankit
print(FirstStudent.course)
# output = DSA

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 SecondStudent:

SecondStudent = Student("Vaibhav", "Cpp")

print(SecondStudent.name)
# output = Vaibhav
print(SecondStudent.course)
# output = Cpp

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.

Also see, Convert String to List Python

Frequently Asked Questions

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:

You can refer to our guided paths on the Coding Ninjas Studio platform. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc.

We hope this blog helped you understand what is the difference between class attributes and instance attributes. To practice and improve yourself in the interview, you can also check out Top 100 SQL problemsInterview experienceCoding interview questions, and the Ultimate guide path for interviews.

Happy Learning!!

Live masterclass