🐍 Python Intermediate

What is Python's property decorator?

Why Interviewers Ask This

Candidates at the intermediate level are expected to not only know this concept but explain the trade-offs involved. Interviewers use this question to see if you can reason about design decisions, not just recall facts.

Answer

The @property decorator allows a method to be accessed like an attribute, implementing getter/setter/deleter logic. Define a getter: @property def full_name(self): return f"{self.first} {self.last}" — access as person.full_name (no parentheses). Define a setter: @full_name.setter def full_name(self, value): self.first, self.last = value.split() — assign as person.full_name = "Alice Smith". Define a deleter: @full_name.deleter def full_name(self): del self.first; del self.last. Properties enforce encapsulation while keeping a clean attribute-access API. Use them for computed attributes, validation on assignment, and lazy-loading. Unlike Java's explicit getters/setters, Python properties let you start with a public attribute and later add logic without changing the API.

Pro Tip

Demonstrate both theoretical understanding and practical experience. Say what it is, then give an example of how you actually used it in a Python codebase.