Python · The language, not just syntax

How does object-oriented programming work in Python?

MediumoopclassesmethodsUpdated Oct 2026
Quiz this track

Short answer

Python classes define attributes and methods on instances and classes. Instance methods take self, classmethods take cls, staticmethods take neither, and properties expose computed attributes without changing the call site.

A class is a callable object that creates instances. __init__ initialises a new instance after creation; it is not the constructor itself, which is __new__. Attributes can live on the instance or on the class, and lookup checks the instance first, then the class and its bases.

python
class Account:
    kind = "checking"

    def __init__(self, owner, balance=0):
        self.owner = owner
        self.balance = balance

    @property
    def is_overdrawn(self):
        return self.balance < 0

    @classmethod
    def empty(cls, owner):
        return cls(owner=owner, balance=0)

    @staticmethod
    def valid_owner(name):
        return bool(name and name.strip())

account = Account.empty("Asha")
print(account.is_overdrawn, Account.valid_owner("Asha"))
The property is read like an attribute. The classmethod builds an alternate constructor. The staticmethod is a namespaced helper.

Encapsulation in Python

  • A single underscore means internal by convention, not enforced
  • A double underscore triggers name mangling to avoid accidental overrides
  • Properties let you add validation without breaking attribute access
  • There is no private keyword; discipline and clear APIs do the work

Prefer composition over deep class hierarchies. A class should have one reason to change, and dependencies should be passed in rather than constructed internally. That makes testing straightforward because a fake can be injected.

A mutable class attribute is shared by all instances. Assign immutable defaults at class level, and create lists and dicts inside __init__.

Follow-ups you should expect

Interviewers use these to check whether the first answer was understanding or memorisation.

  1. 1What is the difference between __init__ and __new__?
  2. 2When would you use a classmethod instead of a staticmethod?
  3. 3How does name mangling work?
  4. 4Why prefer composition over inheritance here?

Say it in the interview

In Python, classes bundle state and behaviour, and methods differ by what they receive: self for instances, cls for the class, and nothing for staticmethods. I use properties to add validation without changing call sites, and classmethods for alternate constructors. Encapsulation is by convention rather than enforcement, so I design small APIs and inject dependencies, which also makes the code easier to test.

Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.

Related questions

Reading is step one

Now say it out loud.

Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Python mock interview and get scored feedback on depth, clarity and confidence.