An object is an instance of a class. Objects are one of the most important concepts in Object Oriented Programming (OOP). An object can contain data, called attributes, and behavior, provided by methods.
An object is a specific instance created from a class. The class defines the structure, while the object contains actual data.
class Student:
name = "Rahul"
student1 = Student()
Here, Student is the class and student1 is an object of that class.
An object is created by calling the class name followed by parentheses.
class Student:
pass
student1 = Student()
print(student1)
The output will show a representation containing the class name and the object's identity information.
Object attributes store information belonging to a particular object. They are commonly created using self.
class Student:
def __init__(self, name, age):
self.name = name
self.age = age
student1 = Student("Rahul", 20)
print(student1.name)
print(student1.age)
Output:
Rahul 20
Multiple objects created from the same class can contain different data.
class Student:
def __init__(self, name, age):
self.name = name
self.age = age
student1 = Student("Rahul", 20)
student2 = Student("Amit", 22)
print(student1.name)
print(student2.name)
Output:
Rahul Amit
Methods are functions defined inside a class. They can operate on the data of an object.
class Student:
def __init__(self, name):
self.name = name
def display(self):
print("Student:", self.name)
student1 = Student("Rahul")
student1.display()
Output:
Student: Rahul
The self parameter refers to the current object. It allows instance methods to access attributes and other methods belonging to that object.
class Student:
def __init__(self, name):
self.name = name
def show(self):
print(self.name)
student = Student("Rahul")
student.show()
Output:
Rahul
The state of an object is represented by the values stored in its attributes.
class Student:
def __init__(self, name, age):
self.name = name
self.age = age
student = Student("Rahul", 20)
print(student.name)
print(student.age)
Here, the object's state includes the student's name and age.
Object attributes can be changed after an object has been created.
class Student:
def __init__(self, name):
self.name = name
student = Student("Rahul")
print(student.name)
student.name = "Amit"
print(student.name)
Output:
Rahul Amit
Python allows an attribute to be added directly to an individual object.
class Student:
pass
student = Student()
student.name = "Rahul"
student.age = 20
print(student.name)
print(student.age)
Output:
Rahul 20
The del keyword can be used to delete an attribute from an object.
class Student:
def __init__(self):
self.name = "Rahul"
self.age = 20
student = Student()
del student.age
print(student.name)
Output:
Rahul
Every object has an identity. The built-in id() function can be used to obtain an integer representing the object's identity during its lifetime.
class Student:
pass
student = Student()
print(id(student))
The exact number depends on the running Python process and may be different each time the program runs.
The type() function tells us the type or class of an object.
class Student:
pass
student = Student()
print(type(student))
Output will be similar to:
<class '__main__.Student'>
The isinstance() function checks whether an object is an instance of a specified class. It returns True or False.
class Student:
pass
student = Student()
print(isinstance(student, Student))
Output:
True
The is operator checks whether two references refer to the same object.
class Student:
pass
student1 = Student()
student2 = student1
print(student1 is student2)
Output:
True
Here, both variables refer to the same object.
class Student:
pass
student1 = Student()
student2 = Student()
print(student1 is student2)
Output:
False
Although both objects are created from the same class, they are different objects.
For many normal Python objects, the __dict__ attribute contains the object's instance attributes.
class Student:
def __init__(self, name, age):
self.name = name
self.age = age
student = Student("Rahul", 20)
print(student.__dict__)
Output:
{'name': 'Rahul', 'age': 20}
The __str__() method can define a user-friendly string representation of an object.
class Student:
def __init__(self, name):
self.name = name
def __str__(self):
return self.name
student = Student("Rahul")
print(student)
Output:
Rahul
The __repr__() method can provide an unambiguous representation of an object and is commonly used when displaying or debugging objects.
class Student:
def __init__(self, name):
self.name = name
def __repr__(self):
return f"Student({self.name!r})"
student = Student("Rahul")
print(repr(student))
Output:
Student('Rahul')
class Calculator:
def add(self, a, b):
return a + b
def multiply(self, a, b):
return a * b
calculator = Calculator()
print(calculator.add(10, 20))
print(calculator.multiply(5, 4))
Output:
30 20
Objects can be passed to functions just like other Python values.
class Student:
def __init__(self, name):
self.name = name
def display_student(student):
print(student.name)
student = Student("Rahul")
display_student(student)
Output:
Rahul
A function can also create and return an object.
class Student:
def __init__(self, name):
self.name = name
def create_student():
return Student("Rahul")
student = create_student()
print(student.name)
Output:
Rahul
| Class | Object |
|---|---|
| Blueprint or template | Instance of a class |
| Defines attributes and methods | Contains actual object data |
| Created using class keyword | Created by calling the class |
| Can create many objects | Represents a particular instance |
Suppose we are developing a school management system. We can create a Student class and then create many student objects.
class Student:
def __init__(self, name, course):
self.name = name
self.course = course
student1 = Student(
"Rahul",
"Python"
)
student2 = Student(
"Amit",
"Java"
)
print(student1.name)
print(student1.course)
print(student2.name)
print(student2.course)
Output:
Rahul Python Amit Java
A simplified object lifecycle can be understood as:
class Student:
def __init__(self, name):
self.name = name
student = Student("Rahul")
print(student.name)
del student
Question: What is an object in Python?