Python Classes and Objects

Last Updated : 22 Jan, 2026

Python’s object-oriented programming (OOP) allows you to model real-world entities in code, making programs more organized, reusable and easier to maintain. By grouping related data and behavior into a single unit, classes and objects help you write cleaner, more logical code for everything from small scripts to large applications.

Class

A class in Python is a user-defined template for creating objects. It bundles data and functions together, making it easier to manage and use them. When we create a new class, we define a new type of object. We can then create multiple instances of this object type.

420046617
Classes and Objects (Here Dog is the Base Class and Bobby is Object)

Creating Class

Classes are created using class keyword. Attributes are variables defined inside class and represent properties of the class. Attributes can be accessed using dot . operator (e.g., MyClass.my_attribute).

Python
# define a class
class Dog:
    sound = "bark"  # class attribute

Object

An object is a specific instance of a class. It holds its own set of data (instance variables) and can invoke methods defined by its class. Multiple objects can be created from same class, each with its own unique attributes.

Let's create an object from Dog class.

Python
class Dog:
    sound = "bark"

dog1 = Dog() # Creating object from class
print(dog1.sound) # Accessing the class

Output
bark

Explanation: sound attribute is a class attribute. It is shared across all instances of Dog class, so can be directly accessed through instance dog1.

Initiate Object with __init__()

The __init__() method acts as a constructor in Python and is automatically executed when an object is created. It is used to initialize the attributes of the object with the values provided at the time of object creation.

Python
class Dog:
    species = "Canine"  # Class attribute

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

# Creating an object of the Dog class
dog1 = Dog("Buddy", 3)

print(dog1.name)  
print(dog1.species)  

Output
Buddy
Canine

Explanation:

  • self refers to the current object and is used to store data inside it.
  • Dog("Buddy", 3) creates an object and passes values to __init__().
  • self.name and self.age store the object’s name and age.
  • dog1.name accesses the object’s instance attribute and dog1.species accesses the shared class attribute.

__str__() Method

__str__ method in Python allows us to define a custom string representation of an object. By default, when we print an object or convert it to a string using str(), Python uses the default implementation, which returns a string like <__main__.ClassName object at 0x00000123>.

Let’s look at an example of using __str__() method to provide a readable string output for an object:

Python
class Dog:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def __str__(self):
        return f"{self.name} is {self.age} years old."
dog1 = Dog("Buddy", 3)
dog2 = Dog("Charlie", 5)

print(dog1)  
print(dog2)  

Explanation:

  • __str__ Implementation: Defined as a method in Dog class. Uses self parameter to access instance's attributes (name and age).
  • Readable Output: When print(dog1) is called, Python automatically uses __str__ method to get a string representation of object. Without __str__, calling print(dog1) would produce something like <__main__.Dog object at 0x00000123>.

Class and Instance Variables in Python

In Python, variables defined in a class can be either class variables or instance variables and understanding distinction between them is crucial for object-oriented programming.

Class Variables

These are variables that are shared across all instances of a class. It is defined at class level, outside any methods. All objects of class share same value for a class variable unless explicitly overridden in an object.

Instance Variables

Variables that are unique to each instance (object) of a class. These are defined within __init__() method or other instance methods. Each object maintains its own copy of instance variables, independent of other objects.

Below code shows how class variables are shared across all objects, while instance variables are unique to each object.

Python
class Dog:
    # Class variable
    species = "Canine"

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

# Create objects
dog1 = Dog("Buddy", 3)
dog2 = Dog("Charlie", 5)

# Access class and instance variables
print(dog1.species)  # (Class variable)
print(dog1.name)     # (Instance variable)
print(dog2.name)     # (Instance variable)

# Modify instance variables
dog1.name = "Max"
print(dog1.name)     # (Updated instance variable)

# Modify class variable
Dog.species = "Feline"
print(dog1.species)  # (Updated class variable)
print(dog2.species)  

Explanation:

  • Class Variable (species): Shared by all instances of class. Changing Dog.species affects all objects, as it's a property of class itself.
  • Instance Variables (name, age): Defined in __init__() method. Unique to each instance (e.g., dog1.name and dog2.name are different).
  • Accessing Variables: Class variables can be accessed via class name (Dog.species) or an object (dog1.species). Instance variables are accessed via object (dog1.name).
  • Updating Variables: Changing Dog.species affects all instances. Changing dog1.name only affects dog1 and does not impact dog2.
Suggested Quiz

0 Questions

Quiz Completed Successfully

Your Score : 0/0

Accuracy : 0%

Comment
Article Tags:

Explore