Interview Q&A · Python · 35 questions in this topic

Object-Oriented Python — Python Interview Q&A

All Python topics

Classes, objects, inheritance and the dunder protocol — including MRO, the diamond problem and how Python implements encapsulation without real access modifiers. Part of Object-Oriented Python · 375 questions in the Python bank.

What are classes and objects in Python?

Answer

A class is a blueprint that defines the structure (attributes) and behavior (methods) shared by a group of objects. An object (instance) is a concrete value created from that blueprint, with its own independent state.

In Python, classes themselves are also objects (instances of type), which is part of why Python's object model is considered unusually consistent — "everything is an object," including classes, functions, and modules.

Example

Example.pypython
class Dog:
    def __init__(self, name):
        self.name = name # instance attribute
    def bark(self):
        return f"{self.name} says Woof!"
rex = Dog("Rex") # rex is an object/instance of Dog
fido = Dog("Fido")
print(rex.bark()) # 'Rex says Woof!'
print(fido.bark()) # 'Fido says Woof!' -- independent state
print(type(rex)) # <class '__main__.Dog'>
print(type(Dog)) # <class 'type'> -- classes are objects too

💡 Mentioning "classes are themselves instances of type " is a small detail that previews metaclass understanding and signals depth beyond textbook OOP definitions.

What is __init__, and when is it called?

Answer

__init__ is the initializer method, automatically called immediately after a new instance is created, used to set up that instance's initial state (its instance attributes).

__init__ is often mistakenly called "the constructor," but the actual object-creation step happens in __new__ (Q115) — __init__ just configures the already-created object. __init__ should not return anything other than None; returning any other value raises a TypeError.

Example

Example.pypython
class User:
    def __init__(self, username, active=True):
        self.username = username
        self.active = active
        print(f"Creating user: {username}")
u = User("ana") # prints "Creating user: ana" during construction
print(u.username, u.active) # ana True

💡 If asked "is __init__ a constructor?", the precise answer is "not exactly — __new__ constructs the object, __init__ initializes it." Bringing this up unprompted is a strong depth signal.

What is the difference between instance variables and class variables?

Answer

An instance variable belongs to one specific object and is usually set in __init__ via self.x =. A class variable is shared by all instances of the class, defined directly in the class body outside any method.

Reading a class variable through an instance works fine (Python looks it up on the instance first, then falls back to the class). But assigning to self.attribute = value always creates a new instance variable that shadows the class variable for that object only — it does not modify the shared class variable.

Example

Example.pypython
class Employee:
    company = "Acme Corp" # class variable -- shared by all instances
    def __init__(self, name):
        self.name = name # instance variable -- unique per object
e1 = Employee("Ana")
e2 = Employee("Ben")
print(e1.company, e2.company) # Acme Corp Acme Corp -- shared
Employee.company = "NewCo" # changes the shared class variable
print(e1.company, e2.company) # NewCo NewCo -- both see the change
e1.company = "Solo LLC" # creates a NEW instance variable on e1 only
print(e1.company, e2.company) # Solo LLC NewCo -- e2 unaffected

Recommended resourcesAffiliate links · may earn us a commission

Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits