Interview Q&A / Python
Python interview questions
Lists, dictionaries, generators, decorators and the Python questions asked in service-company and data roles.
Prefer rapid revision? Practise Python short interview answers →
- What is the Method Resolution Order (MRO) in Python, and how does it work with multiple inheritance?
The MRO is the order in which Python searches a class and its parents for an attribute. It uses C3 linearization: children come before parents, and the order of bases you wrote is kept. You can see it with ClassName.__mro__ or mro().
AdvancedQuick answer1 min read - Why must you define __hash__ when you define __eq__ for objects used in sets or as dict keys?
Objects that compare equal must have the same hash, or sets and dicts will treat them as different. Defining __eq__ without __hash__ sets __hash__ to None, so the class becomes unhashable. Base the hash on the same fields that __eq__ compares.
AdvancedQuick answer1 min read