Skip to content
ComputerCS
Start learning

Python Programming · Lesson 2.8

Object-Oriented Programming in Python

Understand classes, objects, attributes, methods, constructors, encapsulation, inheritance and polymorphism through small Python models.

01

Learning objectives

  • Distinguish a class from an object and an attribute from a method.
  • Define a class and initialise instance state with __init__ and self.
  • Explain encapsulation and why methods protect object invariants.
  • Describe inheritance and polymorphism with a simple example.
02

Lesson notes

Object-oriented programming (OOP) organises a program around objects that combine state and behaviour. A class is the definition or blueprint; an object is one instance created from it. A Student class can define attributes such as name and marks and methods such as add_mark or average. Two Student objects share the same class definition but hold different data.

The constructor method __init__ runs when an object is created. Its first parameter is conventionally named self and refers to the particular instance. Assigning self.name = name stores state on that object. A method call such as student.average() is bound to that object, so Python supplies the instance as self.

Encapsulation groups data with the operations that maintain it. Python does not enforce private fields in the same way as some languages, but a leading underscore communicates an internal implementation detail. A well-designed class validates state at its boundary: marks should remain within an agreed range rather than allowing every caller to create impossible values.

Inheritance allows a specialised class to reuse or extend a base class. A GraduateStudent may inherit common Student behaviour and add research data. Polymorphism means different object types can respond to the same operation through a common interface. Inheritance is useful when there is a genuine “is-a” relationship; composition is often clearer when one object merely uses another.

03

Worked code examples

Worked example

Student is the class, ali is an object, name and marks are instance attributes, and average is an instance method.

class Student:
    def __init__(self, name, marks):
        self.name = name
        self.marks = marks
    def average(self):
        return sum(self.marks) / len(self.marks)

ali = Student("Ali", [78, 84, 90])
print(ali.name, round(ali.average(), 1))
Output
Ali 84.0
04

Key terminology

Class

A definition that describes the state and behaviour of a kind of object.

Object

A runtime instance of a class.

Constructor

The initialisation method, commonly __init__, called when an object is created.

Inheritance

A mechanism for deriving a specialised class from a base class.

05

Common misconceptions

Common mistake

A class and an object are the same thing.

Correct understanding

The class defines a type; each constructed object is a separate instance.

Common mistake

Inheritance should be used to share any code at all costs.

Correct understanding

Use inheritance for genuine specialisation; composition may be a better fit for “has-a” relationships.

06

Check your understanding

1. Which expression creates an instance of Student?

2. What does self refer to in an instance method?

3. Polymorphism allows:

NERDLYY · FEEDBACK

Help shape Nerdlyy

Your feedback goes directly to the team. We read every submission.

No reply needed — you can send feedback without sharing contact details.