Skip to content
Home » Creating objects

Creating objects

Here is a clear, simple, and exam-oriented explanation of Creating Objects in Python.


Creating Objects in Python

An object is an instance of a class.
A class is only a blueprint; to use it, you must create objects.

Objects allow us to store data (attributes) and perform actions (methods) defined inside the class.


What Is an Object? (Exam Definition)

An object is a runtime instance of a class that contains its own data (attributes) and can invoke its own methods.

Creating an object means allocating memory for the attributes and enabling the methods to work on that data.


——————————————

Syntax for Creating an Object

object_name = ClassName(arguments)

When an object is created:

✔ Python calls the __init__ constructor
✔ Attributes are assigned
✔ Memory is allocated


——————————————

Example: Simple Object Creation

class Student:
    def __init__(self, name, roll):
        self.name = name
        self.roll = roll

# Creating objects
s1 = Student("Amit", 101)
s2 = Student("Rita", 102)

print(s1.name, s1.roll)
print(s2.name, s2.roll)

Output:

Amit 101
Rita 102

——————————————

Each Object Has Its Own Data

s1.name = "Amit"
s2.name = "Rita"

Each object maintains separate copies of attributes.


——————————————

Accessing Attributes and Methods

Use the dot operator (.):

object.method()
object.attribute

Example:

s1.display()
print(s1.name)

——————————————

Creating Multiple Objects

p1 = Person("John", 25)
p2 = Person("David", 30)
p3 = Person("Sara", 28)

Objects represent different real-world entities.


——————————————

Example: BankAccount Objects

class BankAccount:
    def __init__(self, acc_no, name, balance=0):
        self.acc_no = acc_no
        self.name = name
        self.balance = balance

# Creating objects
acc1 = BankAccount(101, "Ravi", 5000)
acc2 = BankAccount(102, "Neha", 3000)

print(acc1.name, acc1.balance)
print(acc2.name, acc2.balance)

Each account is a different object with separate values.


——————————————

Objects Calling Methods

acc1.deposit(2000)
acc1.withdraw(1000)
acc1.display()

——————————————

Anonymous Objects (Temporary Objects)

Python also allows creation of objects without storing them in variables:

Student("Raj", 105).display()

Such objects are used temporarily.


——————————————

Checking Type of an Object

print(type(s1))

Output:

<class '__main__.Student'>

——————————————

Exam-Ready 5–6 Line Answer

Objects in Python are instances of classes created using the syntax object_name = ClassName(). When an object is created, Python automatically calls the constructor to initialize its attributes. Each object has its own data and can access methods using the dot operator. Objects represent real-world entities and allow OOP concepts like encapsulation, inheritance, and polymorphism.