×

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 Create Instance Objects

Last Updated : May 03, 2025

Python instance object is an individual object created from a class. An instance object has access to the attributes and methods defined within the class.

In this chapter, we will learn how to create instance objects with the help of examples.

Creating an Instance Object

To create an instance object, you need to call the class as if it were a function. This calls the __init__() method (the constructor) to initialize the object's attributes.

Example

In this example, we define a Person class and create an instance object of it. The instance object will have access to the attributes and methods of the class.

# Defining the class
class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def greet(self):
        print(f"Hello, my name is {self.name} and I am {self.age} years old.")

# Creating an instance object of Person
person1 = Person("Rajesh", 30)
person1.greet()

The output of the above code would be:

Hello, my name is Rajesh and I am 30 years old.

Creating Multiple Instance Objects

You can create multiple instance objects from the same class, each with different attributes.

Example

In this example, we create multiple instances of the Car class, each representing different cars with their own attributes.

# Defining the class
class Car:
    def __init__(self, make, model, year):
        self.make = make
        self.model = model
        self.year = year

    def display(self):
        print(f"{self.year} {self.make} {self.model}")

# Creating instance objects
car1 = Car("Toyota", "Corolla", 2020)
car2 = Car("Honda", "Civic", 2021)

# Displaying the cars
car1.display()
car2.display()

The output of the above code would be:

2020 Toyota Corolla
2021 Honda Civic

Accessing Instance Attributes

Once you have created an instance object, you can access its attributes using the dot (.) operator.

Example

In this example, we create an instance of the Student class and access its attributes.

# Defining the class
class Student:
    def __init__(self, name, grade):
        self.name = name
        self.grade = grade

# Creating an instance object
student1 = Student("Anjali", "A+")

# Accessing instance attributes
print(f"Student: {student1.name}, Grade: {student1.grade}")

The output of the above code would be:

Student: Anjali, Grade: A+

Modifying Instance Attributes

You can modify the instance attributes of an object after it has been created. This can be done directly using the dot operator.

Example

In this example, we modify the age attribute of an instance object of the Person class.

# Defining the class
class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

# Creating an instance object
person1 = Person("Rajesh", 30)

# Modifying instance attribute
person1.age = 31

# Displaying the modified attribute
print(f"Updated age: {person1.name} is now {person1.age} years old.")

The output of the above code would be:

Updated age: Rajesh is now 31 years old.

Creating Instance Objects Using Default Values

You can also create instance objects by providing default values for attributes.

Example

In this example, the Person class has default values for the age attribute.

# Defining the class with default values
class Person:
    def __init__(self, name, age=25):  # Default value for age
        self.name = name
        self.age = age

# Creating instance objects
person1 = Person("Rajesh")
person2 = Person("Neha", 30)

# Displaying the attributes
print(f"{person1.name} is {person1.age} years old.")
print(f"{person2.name} is {person2.age} years old.")

The output of the above code would be:

Rajesh is 25 years old.
Neha is 30 years old.

Exercise

Select the correct option to complete each statement about creating instance objects in Python.

  1. To create an instance of a class in Python, you use the ___ syntax.
  2. When an instance is created, the ___ method is called automatically.
  3. If you want to initialize the attributes of an instance when it is created, you should define them in the ___ method.

Advertisement
Advertisement

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

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