Special Methods & Operator Overloading Interview Questions
Dunder methods for equality, representation, construction, and operator overloading, plus common OOP design patterns.
Question 1: What is `__eq__`?
Ans
__eq__ defines how an object's equality comparison behaves when using ==. Custom classes can implement it to compare meaningful state.
Example
python
class User:
def __init__(self, user_id): self.user_id = user_id
def __eq__(self, other):
return isinstance(other, User) and self.user_id == other.user_id
Important Point
If equality semantics change, review hashing and collection behavior as well.
Question 2: What is `__str__` vs `__repr__`?
Ans
__str__ is intended for a readable user-facing representation, while __repr__ is intended to be an unambiguous developer-oriented representation.
Example
python
class User:
def __repr__(self): return "User(id=1)"
def __str__(self): return "User 1"
u = User()
print(str(u))
print(repr(u))
Important Point
If `__str__` is not defined, object formatting can fall back to `__repr__`.
Question 3: What is name mangling?
Ans
Names beginning with two underscores but not ending with two underscores are transformed internally using the class name, helping avoid accidental name collisions in subclasses.
Example
python
class Account:
def __init__(self):
self.__pin = 1234
Important Point
Name mangling is not a security feature and does not make data cryptographically private.
Question 4: What is method binding?
Ans
When a function is accessed as an instance attribute and is defined as a method on a class, Python creates a bound method that supplies the instance automatically.
Example
python
class User:
def greet(self): return "Hi"
u = User()
print(u.greet())
Important Point
This explains why the instance is passed to `self` even though the call appears to provide no explicit first argument.
Question 5: What is super()?
Ans
super() provides a proxy for accessing parent or next-in-MRO behavior, making cooperative inheritance possible.
Example
python
class A:
def greet(self): return "A"
class B(A):
def greet(self): return super().greet() + " B"
print(B().greet())
Important Point
In multiple inheritance, super() follows the MRO rather than simply meaning 'my direct parent'.
Question 6: What is `__new__`?
Ans
__new__ is a class-level construction hook responsible for creating and returning an instance before __init__ initializes it.
Use `__new__` for specialized construction such as immutable subclasses or controlled instance creation; most classes only need `__init__`.
Question 7: What is `__del__`?
Ans
__del__ is a finalizer hook that may be called when an object is being finalized, but its execution timing and reliability are not suitable for critical resource cleanup.
Example
python
class Demo:
def __del__(self):
print("finalizing")
Important Point
Use context managers and explicit cleanup for resources instead of depending on `__del__`.
Question 8: What is singleton pattern in Python?
Ans
A singleton restricts a class to one shared instance. Python often avoids needing a singleton class by using a module-level object or dependency injection.
Example
python
# module-level instance
config = {"mode": "prod"}
Important Point
Global singleton state can create hidden dependencies and make tests harder, so use it only when the shared lifecycle is truly required.
Question 9: What is operator overloading?
Ans
Python lets classes customize operators through special methods such as __add__, __eq__, and __lt__.
Example
python
class Money:
def __init__(self, value): self.value = value
def __add__(self, other): return Money(self.value + other.value)
print((Money(10) + Money(5)).value)
Important Point
Implement operators with clear, consistent semantics and return NotImplemented when appropriate for unsupported types.
Question 10: What is rich comparison?
Ans
Special methods such as __eq__, __lt__, __le__, __gt__, __ge__, and __ne__ define rich comparisons for custom objects.
Example
python
class Score:
def __init__(self, value): self.value = value
def __lt__(self, other): return self.value < other.value
Important Point
Use functools.total_ordering only when its trade-offs are acceptable; defining the needed comparisons explicitly can be clearer.