Назад к подготовке
ВопросСредняяpython-runtimeСобеседование · Navio

Вопрос

How does @dataclass reduce boilerplate, what does frozen=True do, and how do descriptors or properties relate to attribute access?

Ответить самому

Сначала сформулируйте ответ как на собеседовании, затем откройте разбор и оцените себя.

Загрузка

Короткий ответ

@dataclass generates boilerplate such as __init__, __repr__ and comparisons from annotations. frozen=True prevents normal assignment after initialization. Descriptors customize attribute get/set/delete behavior.

Полный разбор

A dataclass is a class decorator that reads annotated fields and generates common methods, usually including __init__, __repr__ and equality methods. It is a convenience layer over an ordinary Python class, not a new object model.

With frozen=True, the generated class blocks normal attribute reassignment after construction. It is useful for value objects such as points or configuration records. It is not the same as deep immutability: mutable values stored inside can still mutate unless you choose immutable field types.

Descriptors are objects that define methods such as __get__, __set__ and __delete__. Python's attribute access machinery calls them to customize reads and writes. property is a common descriptor-based API. The connection to the interview is that high-level conveniences such as dataclasses and properties ultimately sit on top of Python's attribute model.

Теория

Python class features are mostly protocol and descriptor machinery layered behind convenient syntax.

Типичные ошибки

  • Think type hints enforce runtime types by default.
  • Treat frozen dataclasses as deeply immutable.
  • Confuse class attributes with instance attributes.

Как отвечать на собеседовании

  • Name two methods dataclass generates.
  • Use property as the simplest descriptor example.