Python Abstract Classes and Instance, Class and Static Methods
Company: Engelhart
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Technical Screen
Explain abstract classes and the three kinds of methods a Python class can define. These came as a quick oral sequence in the fundamentals half of a technical screen for a junior quantitative developer role, with follow-ups on why each feature exists.
```python
class Instrument:
def price(self, curve): ...
@classmethod
def from_record(cls, record): ...
@staticmethod
def is_valid_quantity(quantity): ...
```
### Clarifying Questions
- Should the answer use Python's `abc` module specifically, or also compare with abstract classes in languages such as Java or C++?
- Is the interest in how method binding works underneath, in when to use each kind of method, or both?
### Part 1 — Abstract classes
1. What is an abstract class?
2. Can an abstract class be instantiated directly?
```hint What would be missing
Ask what a caller would get if it could create an object whose required methods have no implementation, and at which moment Python checks for that.
```
#### What This Part Should Cover
- The purpose of an abstract class: a shared interface, plus shared implementation, for subclasses
- How Python declares one, and exactly when and how instantiation fails
- What a subclass must do to become instantiable
### Part 2 — Instance, class and static methods
1. What is the difference between an instance method and a class method?
2. What are `self` and `cls`?
3. What is a static method?
4. If a static method receives neither the instance nor the class, why put it in the class at all?
```hint Look at the first argument
For each kind of method, write down what Python passes as the first argument when you call it on an instance and when you call it on the class.
```
#### What This Part Should Cover
- What each kind of method receives implicitly, and therefore what it can access
- What `self` and `cls` are, including that the names are conventions
- Typical uses: per-object behavior, alternative constructors, related helpers
- How each kind behaves under inheritance
### What a Strong Answer Covers
- Correct mechanics of `ABC` and `@abstractmethod`, including that abstract classes may contain concrete methods and state
- A precise account of method binding rather than "class methods belong to the class"
- A use case for each kind of method, with a reason to prefer one over another
- Inheritance consequences, especially for alternative constructors
- A short, correct code example
### Follow-up Questions
- A subclass inherits a class-method alternative constructor. What type of object does it build when called on the subclass, and what would change if it were a static method?
- What happens if a subclass implements only some of the abstract methods?
- When would you choose `typing.Protocol` over an abstract base class?
- A method runs `self.count += 1`, where `count` is a class attribute. What happens to the class attribute?
Overview: A Python object-oriented programming interview question on abstract classes and method kinds. It asks what an abstract class is and whether it can be instantiated, how instance, class and static methods differ, what self and cls refer to, and why a static method would live inside a class at all.
Read the full Engelhart Software Engineer interview experience this question came from