KatInterview

Python ABCs and the Three Kinds of Methods Explained

Data Structure DesignModel answer
MediumModel answerData Structure Designpythonoopabstract-classes

Last updated

Explain how abstract base classes work in Python and how instance, class and static methods differ in binding, inheritance behavior and typical use.

You are asked to explain Python's object-oriented basics, as you would in a spoken technical screen. Use the skeleton below as the reference point:

class Instrument:
    def price(self, curve): ...

    @classmethod
    def from_record(cls, record): ...

    @staticmethod
    def is_valid_quantity(quantity): ...

Part 1: Abstract classes

  1. What is an abstract class, and what is it for?
  2. Can you create an instance of one directly? Say exactly when and how it fails, and what a subclass must do to be instantiable.

Part 2: Kinds of methods

  1. How does an instance method differ from a class method?
  2. What do self and cls refer to? Are they keywords?
  3. What is a static method?
  4. A static method gets neither the instance nor the class. Why define it inside a class instead of at module level?

A strong answer is precise about how methods are bound, gives a use case for each kind, explains what happens under inheritance, and includes a short correct code example. You may mention how other languages differ, but focus on Python's abc module.

Hints

Hint 1

Python does not enforce abstractness on its own. Think about which base class or metaclass and which decorator make instantiation fail, and at what moment the error is raised.

Hint 2

Functions stored in a class are descriptors. Ask what gets passed as the first argument when you access the same function through an instance, through the class, or after wrapping it in classmethod or staticmethod.

Hint 3

For the alternative-constructor question, compare writing `return Instrument(...)` with `return cls(...)` inside a classmethod, then think about what a subclass gets back in each case.