Classes and objects are fundamental concepts of Object-Oriented Programming (OOP) in Python. A class defines the structure and behavior of objects, while an object is an instance created from a class. Classes help organize related data and functions into a single unit. Objects allow us to access and use the properties and methods defined inside a class.
Table of Contents
What is a Class?
A class in Python is a blueprint or template used to create objects. It defines the attributes and methods that objects created from the class can have.
Attributes represent the data or properties of an object, while methods represent the actions or behavior that the object can perform. A class is defined using the class keyword.
For example, a Student class can contain attributes such as name, age, and course, and methods such as display_details().
Example:
# Python program to implement class
class Student:
name = "Tanavi"
age = 20
course = "BSc IT"
def display_details(self):
print("Name:", self.name)
print("Age:", self.age)
print("Course:", self.course)
student = Student()
student.display_details()
Output:
Name: Tanavi
Age: 20
Course: BSc IT
Explanation:
- The Student class is created using the class keyword.
- The class contains three attributes: name, age, and course.
- The display_details() method is defined to display the student's information.
- The student object is created from the Student class.
- The display_details() method is called using the object.
- The method displays the values stored in the class attributes.
Features of a Class
A class provides several features that make it useful for organizing and managing data in Python programs.
- Blueprint for Objects: A class acts as a blueprint or template for creating objects. It defines the common structure and behavior that objects of that class can have. For example, a Car class can define common properties such as brand, model, and color, and methods such as start() and stop().
- Encapsulation of Data and Methods: A class can combine data and the functions that operate on that data into a single unit. This makes programs more organized and easier to manage.
- Code Reusability: Once a class is created, multiple objects can be created from it. The same attributes and methods can therefore be reused by different objects.
- Supports Object-Oriented Programming: Classes provide the foundation for important OOP concepts such as inheritance, polymorphism, encapsulation, and abstraction.
- Improves Program Organization: Classes allow related data and functionality to be grouped together. This makes large programs easier to understand, maintain, and modify.
What is an Object?
An object is an instance of a class. When an object is created, it can access the attributes and methods defined by its class.
A class provides the blueprint, while an object represents an actual entity created using that blueprint.
For example, if Student is a class, then student1 and student2 can be objects of the Student class. Both objects can have the same attributes but different values.
Example:
# Python program to implement objects
class Student:
def __init__(self, name, age):
self.name = name
self.age = age
def display_details(self):
print("Name:", self.name)
print("Age:", self.age)
student1 = Student("Tanavi", 20)
student2 = Student("Rahul", 21)
student1.display_details()
student2.display_details()
Output:
Name: Tanavi
Age: 20
Name: Rahul
Age: 21
Explanation:
- The Student class is created.
- The constructor initializes the name and age attributes.
- Two objects, student1 and student2, are created.
- Each object receives different values.
- Both objects can use the display_details() method.
- Each object maintains its own data.
Features of an Object
Objects have several important features that allow them to represent real-world entities in Python programs.
1. Instance of a Class: An object is an instance of a class. It is created using the class name.
Example:
student1 = Student()
Here, student1 is an object of the Student class.
2. Has Its Own State: An object can store its own data in the form of instance attributes. Different objects of the same class can contain different values.
3. Can Access Methods: An object can call the methods defined in its class.
Example:
student1.display_details()
The object student1 calls the display_details() method.
4. Has Identity: Every object has its own identity in memory. Even if two objects contain the same values, they are still separate objects.
5. Represents a Real-World Entity: Objects can be used to represent real-world entities such as students, employees, cars, products, or bank accounts.

How to Create an Object?
An object is created by calling the class name followed by parentheses.
Syntax:
object_name = ClassName()
If the class constructor requires parameters, the values can be passed inside the parentheses.
object_name = ClassName(arguments)
Example:
# Python program to create an object
class Student:
def __init__(self, name, course):
self.name = name
self.course = course
student1 = Student("Tanavi", "BSc IT")
print("Name:", student1.name)
print("Course:", student1.course)
Output:
Name: Tanavi
Course: BSc IT
Explanation:
- The Student class is defined.
- The __init__() method accepts name and course.
- The student1 object is created using the Student class.
- "Tanavi" and "BSc IT" are passed to the constructor.
- The values are stored in the object's attributes.
- The attributes are accessed using the object.
Accessing Class Attributes
Class attributes are variables defined directly inside a class and outside its methods. They are shared by all objects of that class unless an object has an instance attribute with the same name.
Class attributes can be accessed using the class name or through an object.
For example:
Student.college
or
student1.college
Example:
# Python program to access class attributes
class Student:
college = "Dr. D. Y. Patil College"
def __init__(self, name):
self.name = name
student1 = Student("Tanavi")
student2 = Student("Rahul")
print("Student 1:", student1.name)
print("College:", Student.college)
print("Student 2:", student2.name)
print("College:", student2.college)
Output:
Student 1: Tanavi
College: Dr. D. Y. Patil College
Student 2: Rahul
College: Dr. D. Y. Patil College
Explanation:
- The Student class contains a class attribute named college.
- The __init__() method creates an instance attribute named name.
- Two objects are created from the Student class.
- Both objects have different names.
- The college attribute is shared by the class.
- The class attribute can be accessed using either the class name or an object.
Accessing Instance Attributes
Instance attributes belong to individual objects. They are usually defined using the self parameter inside the class. Each object can have different values for its instance attributes.
For example:
student1.name
Example:
# Python program to access instance attributes
class Student:
def __init__(self, name, age):
self.name = name
self.age = age
student1 = Student("Tanavi", 20)
student2 = Student("Rahul", 21)
print("Student 1:", student1.name, student1.age)
print("Student 2:", student2.name, student2.age)
Output:
Student 1: Tanavi 20
Student 2: Rahul 21
Explanation:
- The Student class defines name and age as instance attributes.
- student1 receives the values "Tanavi" and 20.
- student2 receives the values "Rahul" and 21.
- Each object stores its own values.
- The attributes are accessed using the corresponding object.
Calling Methods Using an Object
Methods are functions defined inside a class. Objects can use these methods to perform actions defined by the class. The object name is used to call a method.
For example:
student1.display_details()
Example:
# Python program to call methods using an object
class Calculator:
def add(self, a, b):
return a + b
def multiply(self, a, b):
return a * b
calculator = Calculator()
print("Addition:", calculator.add(10, 5))
print("Multiplication:", calculator.multiply(10, 5))
Output:
Addition: 15
Multiplication: 50
Explanation:
- The Calculator class contains two methods.
- The add() method returns the sum of two numbers.
- The multiply() method returns the product of two numbers.
- A calculator object is created.
- Both methods are called using the object.
- The results are displayed.
Difference Between Class and Object
A class and an object are closely related, but they are not the same. A class defines the structure and behavior, while an object is an actual instance created from that class.
| Basis of Comparison | Class | Object |
|---|---|---|
| Definition | A class is a blueprint or template for creating objects. | An object is an instance of a class. |
| Purpose | It defines the attributes and methods that objects can have. | It uses the attributes and methods defined by the class. |
| Creation | A class is defined using the class keyword. | An object is created by calling the class. |
| Memory | Defining a class does not create an individual object instance. | Creating an object allocates memory for that instance. |
| Data | A class can define class attributes and methods. | An object can contain its own instance attributes. |
| Behavior | A class defines the behavior through methods. | An object performs behavior by calling those methods. |
| Number | One class can be used to create many objects. | Many objects can be created from one class. |
| Example | class Student: | student1 = Student() |
| Relationship | The class acts as a blueprint. | The object is created from the blueprint. |
| Real-World Representation | A class represents a general category or design. | An object represents a specific entity belonging to that category. |
Common Beginner Mistakes
Beginners often make mistakes while defining classes, creating objects, and accessing their attributes and methods. Understanding these mistakes can help avoid common errors.
1. Forgetting the self Parameter: Instance methods should include self as their first parameter.
Wrong:
def display_details():
Correct:
def display_details(self):
Without self, calling the method through an object can result in an error.
2. Forgetting to Create an Object: Defining a class does not automatically create an object.
Wrong:
class Student:
def display(self):
print("Student")
display()
Correct:
student1 = Student()
student1.display()
The method should be called through an object when it is an instance method.
3. Accessing an Instance Attribute Through the Class: Instance attributes belong to objects, not directly to the class.
Wrong:
class Student:
def __init__(self, name):
self.name = name
print(Student.name)
Correct:
student1 = Student("Tanavi")
print(student1.name)
The name attribute belongs to the student1 object.
4. Forgetting Parentheses When Creating an Object: The class must be called using parentheses when creating an object.
Wrong:
student1 = Student
Correct:
student1 = Student()
Without parentheses, student1 refers to the class itself rather than creating an instance.
5. Confusing Class Attributes with Instance Attributes: Class attributes are generally shared by instances, while instance attributes belong to individual objects.
Example:
class Student:
college = "ABC College"
``
def __init__(self, name):
self.name = name
Here, college is a class attribute, while name is an instance attribute.
Conclusion
Classes and objects are the foundation of Object-Oriented Programming in Python. A class provides a blueprint that defines the attributes and methods, while an object is an instance created from that class. Classes help organize related data and functionality into a single unit, while objects allow programmers to create multiple independent instances using the same class structure. Python also provides features such as constructors, instance attributes, class attributes, and methods to make classes more useful. Understanding the difference between classes and objects is essential before learning advanced OOP concepts such as inheritance, polymorphism, encapsulation, and abstraction. By practicing with simple classes and objects, beginners can build a strong foundation for developing structured and reusable Python programs.
Frequently Asked Questions (FAQs)
1. What is a class in Python?
A class is a blueprint or template used to create objects. It defines the attributes and methods that objects of the class can use.
2. What is an object in Python?
An object is an instance of a class. It contains its own data and can access the methods defined by its class.
3. How do you create an object in Python?
An object is created by calling the class name.
student1 = Student()If the constructor requires arguments, they can be passed while creating the object.
student1 = Student("Tanavi", 20)
4. What is the difference between a class and an object?
A class is a blueprint that defines the structure and behavior, while an object is an actual instance created from that class.
5. Can one class have multiple objects?
Yes. A single class can be used to create multiple objects. Each object can have different instance attribute values.
6. What is the purpose of self in a class?
self refers to the current object. It is commonly used to access and store instance attributes and call instance methods.
7. What are class attributes?
Class attributes are variables defined directly inside a class and outside its methods. They are generally shared by all instances of that class.
8. What are instance attributes?
Instance attributes are attributes that belong to individual objects. They are commonly created using self inside methods such as __init__().
9. Can an object access methods defined inside a class?
Yes. An object can call the methods defined in its class using the dot operator.
student1.display_details()
10. Can a class have both class attributes and instance attributes?
Yes. A class can contain class attributes that are shared across instances, and instance attributes that store data specific to individual objects.
0 Comments