×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

Python Class and Instance Attributes

Last Updated : May 03, 2025

In Python, class attributes and instance attributes are used to store data in objects. Class attributes are shared by all instances of the class, whereas instance attributes are specific to each object.

In this chapter, we will learn the difference between class and instance attributes with the help of examples.

Instance Attributes

Instance attributes are defined inside the __init__ method using self. These attributes are unique to each object.

Example

In this example, each student object has its own name and age.

# Defining a class with instance attributes
class Student:
    def __init__(self, name, age):
        self.name = name
        self.age = age

# Creating objects
student1 = Student("Aarav", 20)
student2 = Student("Bhavya", 22)

# Accessing instance attributes
print(student1.name, student1.age)
print(student2.name, student2.age)

The output of the above code would be:

Aarav 20
Bhavya 22

Class Attributes

Class attributes are defined directly inside the class and outside any methods. They are shared among all instances of the class.

Example

In this example, all student objects share the same school attribute.

# Defining a class with a class attribute
class Student:
    school = "Greenwood High"  # Class attribute

    def __init__(self, name):
        self.name = name  # Instance attribute

# Creating objects
student1 = Student("Charu")
student2 = Student("Deepak")

# Accessing class and instance attributes
print(student1.name, "-", student1.school)
print(student2.name, "-", student2.school)

The output of the above code would be:

Charu - Greenwood High
Deepak - Greenwood High

Modifying Instance Attributes

Instance attributes can be modified individually for each object without affecting others.

Example

In this example, we change the age of one student.

# Defining a class
class Student:
    def __init__(self, name, age):
        self.name = name
        self.age = age

# Creating objects
student1 = Student("Esha", 21)
student2 = Student("Farhan", 23)

# Modifying instance attribute
student1.age = 22

# Displaying results
print(student1.name, student1.age)
print(student2.name, student2.age)

The output of the above code would be:

Esha 22
Farhan 23

Modifying Class Attributes

If you modify a class attribute using the class name, the change is reflected across all instances that have not overridden that attribute.

Example

In this example, we modify the class attribute school for all students.

# Defining a class
class Student:
    school = "Greenwood High"

    def __init__(self, name):
        self.name = name

# Modifying class attribute
Student.school = "Sunrise Academy"

# Creating objects
student1 = Student("Gauri")
student2 = Student("Harsh")

# Displaying results
print(student1.name, "-", student1.school)
print(student2.name, "-", student2.school)

The output of the above code would be:

Gauri - Sunrise Academy
Harsh - Sunrise Academy

Overriding Class Attributes with Instance Attributes

If an attribute is set on an object with the same name as a class attribute, it overrides the class attribute for that object only.

Example

In this example, we override the class attribute for one object.

# Defining a class
class Student:
    school = "Greenwood High"

    def __init__(self, name):
        self.name = name

# Creating objects
student1 = Student("Isha")
student2 = Student("Jay")

# Overriding class attribute for one instance
student1.school = "DPS Bangalore"

# Displaying results
print(student1.name, "-", student1.school)
print(student2.name, "-", student2.school)

The output of the above code would be:

Isha - DPS Bangalore
Jay - Greenwood High

Python Class and Instance Attributes Exercise

Select the correct option to complete each statement about class and instance attributes in Python.

  1. Attributes defined directly inside a class but outside any methods are called ___ attributes.
  2. Attributes that are unique to each object are called ___ attributes.
  3. To define an instance attribute, you typically assign it using ___ within the __init__ method.

Advertisement
Advertisement

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.