Introduction
In Python, Classes and objects are fundamental concepts that help developers create scalable, maintainable, and reusable applications. Almost every real-world application, from banking systems and e-commerce websites to games and mobile apps, uses classes and objects.
For example, consider a car. A car has properties such as color, brand, and model, and it can perform actions like start, stop, and accelerate. In Python, the car can be represented as a class, while individual cars are objects created from that class.
What are Classes in Python?
A class is a blueprint or template used to create objects.
A class defines:
- Attributes (variables)
- Methods (functions)
Think of a class as a design or blueprint of a house. The blueprint itself is not a real house, but it provides instructions for building one.
Example:
class Student:
pass
Here, Student is a class.
The pass keyword indicates that the class currently contains no attributes or methods.
What are Objects in Python?
An object is an instance of a class.
Once a class is created, you can create multiple objects from it.
Example:
class Student:
pass
student1 = Student()
student2 = Student()
In this example:
- Student is the class.
- student1 and student2 are objects.
Each object is independent and can store its own data.
Syntax of a Class
Basic syntax:
class ClassName:
def method_name(self):
pass
Example:
class Student:
def display(self):
print("Student information")
Creating an Object
Objects are created using the class name followed by parentheses.
class Student:
pass
student = Student()
Here:
student is an object of the Student class.
The init() Method
The __init__() method is called automatically when an object is created.
It is also known as the constructor.
Example:
class Student:
def __init__(self):
print("Object Created")
student = Student()
Output:
The constructor initializes object data.
Example: Class with Attributes
class Student:
def __init__(self, name, age):
self.name = name
self.age = age
student1 = Student("John", 20)
print(student1.name)
print(student1.age)
Output:
20
Explanation:
- name and age are attributes.
- self refers to the current object.
- Values are assigned during object creation.
Understanding self Keyword
The self keyword represents the current object.
Example:
class Employee:
def __init__(self, name):
self.name = name
employee = Employee("David")
print(employee.name)
Output:
Without self, object attributes cannot be accessed properly.
Adding Methods to a Class
Methods define the behavior of objects.
Example:
class Student:
def __init__(self, name):
self.name = name
def greet(self):
print("Hello", self.name)
student = Student("John")
student.greet()
Output:
Explanation:
The greet() method uses the object’s data to display a personalized message.
Multiple Objects from a Class
One class can create many objects.
class Student:
def __init__(self, name):
self.name = name
student1 = Student("John")
student2 = Student("Emma")
student3 = Student("David")
print(student1.name)
print(student2.name)
print(student3.name)
Output:
Emma
David
Each object stores separate data.
Modifying Object Attributes
Attributes can be changed after object creation.
class Student:
def __init__(self, name):
self.name = name
student = Student("John")
student.name = "Michael"
print(student.name)
Output:
Deleting Object Attributes
Use the del keyword.
class Student:
def __init__(self, name):
self.name = name
student = Student("John")
del student.name
The attribute is removed from the object.
Deleting Objects
class Student:
pass
student = Student()
del student
The object is deleted from memory.
Real-Life Examples:
1. Student Management System
class Student:
def __init__(self, name, marks):
self.name = name
self.marks = marks
def display(self):
print("Name:", self.name)
print("Marks:", self.marks)
student1 = Student("John", 85)
student2 = Student("Emma", 92)
student1.display()
student2.display()
Output:
Marks: 85
Name: Emma
Marks: 92
Explanation:
The class stores student information and provides a method to display it.
2. Bank Account
class BankAccount:
def __init__(self, owner, balance):
self.owner = owner
self.balance = balance
def deposit(self, amount):
self.balance += amount
def show_balance(self):
print("Balance:", self.balance)
account = BankAccount("John", 5000)
account.deposit(2000)
account.show_balance()
Output:
This demonstrates how classes model real-world systems.
Class Attributes vs Instance Attributes
Instance Attribute
Unique to each object.
class Student:
def __init__(self, name):
self.name = name
Class Attribute
Shared among all objects.
class Student:
school = "ABC School"
Example:
class Student:
school = "ABC School"
def __init__(self, name):
self.name = name
student1 = Student("John")
student2 = Student("Emma")
print(student1.school)
print(student2.school)
Output:
ABC School
Advantages of Classes and Objects
| Advantage | Description |
|---|---|
| Reusability | Create multiple objects from one class |
| Encapsulation | Keep data and methods together |
| Scalability | Manage large applications easily |
| Organization | Structured code design |
| Maintainability | Easier debugging and updates |
Common Mistakes
1. Forgetting self
Incorrect:
class Student:
def display():
print("Hello")
Correct:
class Student:
def display(self):
print("Hello")
2. Not Using init()
Incorrect:
class Student:
pass
Better:
class Student:
def __init__(self, name):
self.name = name
3. Accessing Undefined Attributes
Incorrect:
student.age
when age doesn’t exist.
Always define attributes first.
4. Creating Objects Incorrectly
Incorrect:
student = Student
Correct:
student = Student()
5. Using Class Variables Instead of Instance Variables
Incorrect:
class Student:
name = ""
Better:
class Student:
def __init__(self, name):
self.name = name
Best Practices
1. Use Meaningful Class Names
class Employee:
instead of:
class E:
2. Initialize Data with init()
def __init__(self, name):
self.name = name
3. Keep Methods Focused
Each method should perform a single task.
4. Follow Naming Conventions
Class names:
BankAccount
StudentRecord
EmployeeData
Use PascalCase.
5. Use Objects for Real-World Entities
Classes should represent meaningful entities such as:
- Student
- Employee
- Product
- Customer
- Vehicle
Conclusion
Classes and objects are the foundation of Object-Oriented Programming in Python. A class acts as a blueprint that defines attributes and methods, while objects are real instances created from that blueprint.
By using classes and objects, developers can create organized, reusable, and scalable applications that closely model real-world entities.
Python Classes and Objects – Interview Questions
Q 1: What is a class in Python?
Q 2: What is an object in Python?
Q 3: How do you define a class?
Q 4: What is the __init__() method?
Q 5: Can objects have multiple attributes?
Python Classes and Objects – Objective Questions (MCQs)
Q1. What is a class in Python?
Q2. What is an object in Python?
Q3. Which keyword is used to define a class in Python?
Q4. What is the purpose of the __init__() method in a class?
Q5. How do you access object attributes in Python?