Interview Q&A / Python

INTERVIEW Q&A · 81 QUESTIONS

Python interview questions

Lists, dictionaries, generators, decorators and the Python questions asked in service-company and data roles.

  1. How do you make your own class work with +, len() and indexing in Python?

    Implement the matching dunder methods: __add__ for +, __len__ for len(), and __getitem__ for obj[i]. Python translates the syntax into calls to these methods, so no inheritance from built-in types is needed.

    IntermediateQuick answer1 min read
  2. What does super() do in Python, and why call super().__init__()?

    super() returns a proxy that looks up methods on the next class in the MRO. Calling super().__init__() runs the parent's initialiser so the inherited setup is not skipped. If a child defines __init__ and does not call it, the parent's attributes are never created.

    IntermediateQuick answer1 min read
  3. What does the @property decorator do in Python?

    @property turns a method into a read-only attribute, so obj.area calls the method without parentheses. Adding @name.setter lets you validate or compute on assignment while callers still use plain attribute syntax.

    IntermediateQuick answer1 min read
  4. What is the difference between @classmethod, @staticmethod and an instance method?

    An instance method receives self, the object. A classmethod receives cls, the class, and is commonly used for alternative constructors. A staticmethod receives neither and is just a function living in the class namespace.

    IntermediateQuick answer1 min read
  5. What is the difference between a class attribute and an instance attribute, and what does this code print?

    A class attribute is shared by all instances. An instance attribute belongs to one object. Assigning through an instance, like a.count = 5, creates a new instance attribute that shadows the class one, but mutating a shared list changes it for everyone.

    IntermediateQuick answer1 min read