In Python, an object is an instance of a class. It represents a real-world entity with attributes (data/properties) and behavior (methods/functions) defined by its class. Everything in Python is an object, including numbers, strings, lists, and even functions.
A class is like a blueprint, and an object is a tangible entity created from that blueprint.
Example: #
filename.python
# Define a class
class Employee:
def __init__(self, name, salary):
self.name = name
self.salary = salary
def work(self):
print(f"{self.name} is working.")
# Create objects (instances of the class)
emp1 = Employee("Aswini", 5000)
emp2 = Employee("Kumar", 10000)
# Access attributes and methods
print(emp1.name) # Aswini
emp2.work() # Kumar is working.
Here:
Employeeโ class (blueprint)emp1andemp2โ objects (instances) of theEmployeeclass
Key Points about Objects #
- Encapsulation: Objects hold their own data (
name,salary) and methods (work()), keeping them self-contained. - Identity, Type, Value: Each object has:
- Identity โ unique identifier in memory (
id(obj)) - Type โ the class it belongs to (
type(obj)) - Value โ the data it holds
- Identity โ unique identifier in memory (
- Dynamic: Python objects can have attributes added, modified, or deleted at runtime.
Real-World Example #
In a library management system:
- Class โ
Book - Object โ
book1with attributes liketitle="Python 101"andauthor="Aswini" - Methods โ
issue(),return_book()
This approach makes the system modular, reusable, and easy to scale.
