CBSE 2026 results are out, Mukul scored a perfect 100/100 in Computer ScienceSee all toppers →

KwickAcademy Python · 8 min · free

Encapsulation and Polymorphism in Python

8 min4 KwickClipsFull text belowFree
Next lesson →Kajal Ma'am (MCA), teaching since 2004Remembered in this browser

Learn encapsulation and polymorphism in Python: underscore names, name mangling, @property and setters, one method across classes, and duck typing.

On screen in this lesson

Words we need

Class: a design; object: a thing made from it
Attribute: data inside an object
Method: a function inside a class
self: the object that is using the method

What is encapsulation?

Keep data and its methods together in one class
Outside code uses methods, not the raw data
Methods can check a value before storing it
Like an ATM: you press buttons, not open the safe

The underscore convention

NameMeaningCan outside use it?
balancepublicyes, freely
_balanceinternalyes, but shouldn't
__balancename changednot by this name

Pause and predict

w.add(-90) returns? 500
w.add(0) returns? still 500
The method guards the data

What is a property?

Looks like an attribute: item.cost
Actually runs a method in the background
@property makes the reading method
@cost.setter makes the checking method

What is polymorphism?

Poly means many; morph means form
Same method name in different classes
Each class does its own version of the work
One loop can handle all the objects

Quick answers

Does Python have a private keyword?

No. It uses underscores: one means internal by convention, two make Python rename the attribute.

What is duck typing?

Python only checks whether the object has the method being called, not its class.

KwickClips from this lesson

Short clips, one idea each. Good for revision the night before.

The full lesson, in text

Hello students, welcome to Kwickprep. Can anyone reach into your wallet app and set the balance to minus five hundred? A good program guards against that. Today we will learn encapsulation, properties, polymorphism and duck typing, with small projects.

First, a quick reminder of four words. A class is a design, and an object is a real thing made from that design. An attribute is data stored inside an object, like a balance. A method is a function written inside a class. The word self means the object that is using the method right now.

Encapsulation means packing data, and the methods that use it, together inside one class. Code outside the class should use the methods, not touch the data directly. This matters because a method can check a value before storing it. Think of an A T M. You press buttons, and the machine checks everything, but you never open the cash safe yourself.

Python has no strict private keyword, so it uses underscores in names. A plain name like balance is public, and anyone can use it. One underscore in front means internal, a polite message that says, do not touch this from outside. Python still allows it, so it is a convention, which means an agreed habit. Two underscores in front make Python change the name, so outside code cannot use that name directly.

Here is a wallet. Dunder init, with two underscores on each side, runs when the object is made, and sets the balance to zero. Underscore bal means balance, kept internal. The add method checks, is the amount greater than zero? Only then, plus equals adds it to the balance. Then add returns the new balance, so it prints five hundred. Notice that code outside never touches underscore bal.

Pause and predict, with the same wallet holding five hundred. We call add with minus ninety. The balance stays five hundred, because minus ninety is not greater than zero. Now the edge value, add with zero. It still stays five hundred, because zero is not greater than zero. The method guards the data, and that is encapsulation at work.

Now two underscores, for the secret three digit code on a debit card, the card verification value. Python quietly renames it, and this is called name mangling. The function hasattr asks, does the object have an attribute with this name? The answer is False. So writing c dot underscore underscore cvv gives an attribute error. But the new name, underscore Card, underscore underscore cvv, still works. So Python protects against mistakes, not against thieves.

Calling a method every time can feel clumsy. A property solves this. It looks like a normal attribute when you use it. But behind the scenes, Python runs a method. The line at property, placed above a method, makes the method that reads the value. The line at cost dot setter makes the method that checks and stores a new value.

Here, the parameter m brings in the marks, stored as underscore marks. The marks method has at property above it. So we write dot marks, with no brackets, and it prints eighty eight. There is no setter here. So if you try to set the marks to ninety five, Python gives an attribute error. The marks are read-only.

Now a shop item with a cost. The parameter v is the value, and underscore c stores the real cost. The setter uses abs, which removes a minus sign, so a cost typed by mistake as minus twenty becomes twenty. Notice that init also says self dot cost equals v. That line goes through the setter too. So the check runs even when the object is first made, and it prints twenty.

The second big idea is polymorphism. The word comes from Greek, where poly means many and morph means form. Different classes have a method with the same name. Each class does the work in its own way. So one loop can call that method on every object, without asking what type it is.

In our example, a bus ride costs twenty rupees and a metro ride costs forty. Both classes have a method called fare. The list holds one Bus object and one Metro object. The loop calls fare on each one. Python runs the right version for each object, and prints twenty, then forty.

Polymorphism often comes with inheritance. Square is a child of Shape, and it overrides area. Here, the attribute s is the side of the square. Double star means power, so side double star two is side into side. A plain shape has an area of zero. A square with side four has an area of sixteen. The same line, shape dot area, gave two different answers.

Python goes one step further, with an idea called duck typing. The saying is, if it walks like a duck and quacks like a duck, treat it as a duck. Python only checks, does this object have the method being called? It does not care about the class, or whether the classes are related. If the method is missing, Python stops with an attribute error.

Duck and Robot have no common parent at all. The function play only needs something with a sound method. A duck gives Quack. A toy robot gives Beep. Both work, because both can make a sound. Python's own len function works the same way, on a string, a list or a dictionary.

Many students learn Java too, so let us compare. To hide data, Python uses the underscore convention, while Java uses the private keyword, which is strict. To control access, Python uses properties, while Java writes get and set methods. For polymorphism, Python accepts any object with the method, while Java needs a shared parent class or interface.

Let us revise what we learned today. Encapsulation keeps data and methods together, and methods guard the data. One underscore means internal by convention, and two underscores make Python change the name. A property reads a value like an attribute, and its setter checks a value before storing it. Polymorphism means the same method name does different work in different classes. And duck typing means Python only asks whether the method exists.

Courses that teach this

CourseUnit
ICSE Class 10 Computer ApplicationsEncapsulation
Programming All levels PythonObject-Oriented Programming
Programming All levels JavaObject-Oriented Foundations

Voice-over in this lesson is AI-generated. The script is written and checked by Kajal Ma'am. Boards can revise a syllabus mid-year, so confirm anything you plan around against the official board circular. Keep your passwords, OTPs and ID numbers to yourself — we never ask for them. To reach Kajal Ma'am, use the WhatsApp button; sharing your number there is how we call you back.

Free to watch, no sign-up. Live classes with Kajal Ma'am are the paid course; these lessons stay free either way.

Want a plan that actually fits your board dates?

Ask Kajal Ma'am directly, 20+ years teaching computer science. Free demo class first, no payment.

Talk to Kajal Ma'am on WhatsApp

Or see the Class 12 Computer Science course →

Studying outside India?

We coach CBSE, IGCSE & international students across the globe, one-to-one, in your local time zone.

Visit International →