Questions
5 of 19
1How would you make a custom class usable in a for loop?
2How do __enter__ and __exit__ work together to implement the context manager protocol?
3How does Python implement inheritance, and what is the Method Resolution Order (MRO)?
4What are dataclasses, and how do they reduce boilerplate compared to manually writing __init__, __eq__, and __repr__?
5What is the @property decorator, and what problem does it solve compared to plain getter/setter methods?
6What is the difference between method overriding and method overloading, and why doesn't Python natively support overloading?
7What problem does the diamond inheritance pattern cause, and how does Python's MRO resolve it?
8What is the difference between a class and an instance in Python?
9How does Python implement encapsulation given that it has no true 'private' access modifiers?
10What is the purpose of self in instance methods, and why must it be explicitly declared in Python?
11What is the difference between __str__ and __repr__, and what convention should __repr__ follow?
12What is a metaclass, and what is type's relationship to every class in Python?
13What is the purpose of __eq__ and __hash__, and why must they be implemented consistently?
14What is a descriptor, and how do @property and ORMs like Django's models rely on the descriptor protocol?
15What is the difference between an instance method, a class method, and a static method?
16What is composition, and why is 'favor composition over inheritance' often recommended?
17What are dunder (magic) methods, and how do they enable operator overloading?
18When would you use a metaclass instead of a class decorator or __init_subclass__?
19How do abstract base classes (abc.ABC) enforce interface contracts in Python?
05 / 19

What is the @property decorator, and what problem does it solve compared to plain getter/setter methods?

Difficulty: 5/10
Properties, Descriptors, Encapsulation

@property turns methods into attribute access with validation and lazy computation

A property is a descriptor that lets a method be accessed through normal attribute syntax. Its main value is API evolution: you can start with a plain attribute, and later replace it with a computed property without breaking callers, because obj.x still works. It also centralizes validation in a setter and lets you expose a read-only attribute by defining only the getter. The trade-off is that attribute access now runs arbitrary code, which can surprise consumers if it is slow, raises, or has side effects. Properties should be cheap and side-effect-free; anything expensive should be an explicit method.

  1. 1

    Use @property for computed values, validation, and read-only attributes.

  2. 2

    Use @x.setter for validated assignment and @x.deleter for controlled deletion.

  3. 3

    Use functools.cached_property for expensive computations that should be memoized per instance.

  4. 4

    Trade-off: properties hide cost. If accessing the attribute can be slow or fail, prefer a method with a descriptive name.

  5. 5

    Common mistake: using properties for things that mutate global state or perform IO. That violates the principle of least surprise.

  6. 6

    Common mistake: forgetting to make the setter validate and assuming the backing attribute is protected by the property alone.

  7. 7

    Version note: @property is ancient and stable; cached_property arrived in 3.8, and 3.8+ also improved documentation via doc on setters.

javascript

Scenario Questions

0-2 years experience

  1. 1You want obj.area instead of obj.get_area(). Which decorator?
  2. 2How do you make an attribute read-only with a property?

2-5 years experience

  1. 1You need to validate that age is non-negative on assignment. How do you implement it?
  2. 2You add a property that calls a remote API. Why is this a bad idea?

5-8 years experience

  1. 1You have an expensive computed attribute that is accessed repeatedly. How do you memoize it per instance?
  2. 2You are evolving a public API from a plain attribute to a property. What compatibility concerns arise?

8+ years experience

  1. 1Design a configuration class where every attribute is validated, cached, and observable, using properties and descriptors without exploding boilerplate.
  2. 2Explain how properties, cached_property, and __set_name__ interact with descriptors and slots in a deeply hierarchical model.

Follow-up Questions

  • When should a computed value be a method rather than a property?
  • How does cached_property differ from property in terms of storage and invalidation?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.