MyInternships.in

Python OOP

Python Classes and Objects

A class is a blueprint for creating objects, and an object is a real instance built from that blueprint. Object-oriented programming (OOP) lets you group related data and behaviour together.


Why Classes and Objects?

So far you may have stored related data in separate variables or a dictionary. A class bundles data (attributes) and behaviour (methods) into one reusable unit, which makes larger programs much easier to organise and maintain.

Defining a simple class
Python
class Student:
    def __init__(self, name, marks):
        self.name = name
        self.marks = marks

    def greet(self):
        print(f"Hi, I am {self.name} and I scored {self.marks}")

The class keyword starts a class definition, and by convention class names use CapitalizedWords. Inside the class, functions defined are called methods, and the special __init__ method runs automatically when a new object is created.

Creating Objects (Instances)

An object, also called an instance, is created by calling the class name like a function. Each object gets its own copy of the attributes defined inside __init__.

Creating and using instances
Python
s1 = Student("Aarav", 88)
s2 = Student("Diya", 92)

s1.greet()
s2.greet()
print(s1.name, s2.marks)
Output
Output
Hi, I am Aarav and I scored 88
Hi, I am Diya and I scored 92
Aarav 92

Understanding self

self refers to the specific object a method is being called on. It is always the first parameter of an instance method, and Python passes it automatically — you never pass it yourself when calling s1.greet().

💡

Think of a class as a cookie cutter and objects as the cookies. The shape (attributes and methods) is the same, but each cookie (object) is a separate piece of data in memory.

TermMeaning
classblueprint that defines attributes and methods
object / instancea concrete thing built from the class
attributea variable that belongs to an object
methoda function that belongs to a class
ℹ️

Classes are the foundation for the rest of OOP in Python: constructors, inheritance, polymorphism, encapsulation, abstraction and dunder methods all build on this idea.

Frequently Asked Questions

What is the difference between a class and an object in Python?+

A class is a blueprint or template that defines what attributes and methods its objects will have. An object (instance) is an actual value created from that blueprint, stored in memory with its own data.

Do I always need self as the first parameter?+

Yes, for regular instance methods. self lets the method access and modify the specific object it was called on. Python fills it in automatically — you only add the other arguments when calling the method.

Can a class have no attributes or methods?+

Yes. You can write class Empty: pass to create a minimal class with no body, then still create objects from it and add attributes later, though defining __init__ is the normal, cleaner approach.

Related Python Topics

Keep learning with these closely related lessons.

Ready to use your Python skills?

Find Python, data science and software internships and fresher jobs across India.

Browse Python Internships