By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
The @property decorator in Python is a powerful tool that allows you to define methods that can be accessed like attributes. This is crucial for encapsulating data and providing controlled access to an object's properties. In real-world applications, it enhances code readability and maintainability. For instance, incorrect usage can lead to bugs that are hard to trace, such as unintended side effects when accessing or modifying an object's state.
python class Circle: def __init__(self, radius): self._radius = radius
⚠️ Common pitfall: Directly accessing private attributes can lead to unintended modifications.
Add a Getter Method
Example: ```python class Circle: def init(self, radius): self._radius = radius
@property def radius(self): return self._radius
``` - Underlying principle: The getter method allows controlled access to the attribute.
Add a Setter Method
@property def radius(self): return self._radius @radius.setter def radius(self, value): if value < 0: raise ValueError("Radius cannot be negative") self._radius = value
``` - Underlying principle: The setter method allows for validation before modifying the attribute.
Access and Modify Attributes
python circle = Circle(5) print(circle.radius) # Output: 5 circle.radius = 10 print(circle.radius) # Output: 10
Experts view the @property decorator as a tool for encapsulation and controlled access. They think in terms of maintaining the integrity of an object's state and providing a clean, intuitive interface for interacting with the object. Instead of directly manipulating attributes, they use getters and setters to enforce rules and validate data.
Exam trap: Questions that require understanding the difference between direct access and using getters/setters.
The mistake: Forgetting to use the @property decorator.
Exam trap: Code snippets that omit the decorator.
The mistake: Not validating input in setter methods.
Exam trap: Questions that require identifying potential issues with setter methods.
The mistake: Using the wrong decorator for setter methods.
Scenario: You are developing a class to represent a bank account. The account balance should never be negative.Question: How would you implement the getter and setter methods for the balance attribute? Solution: 1. Define the class with a private attribute for balance.2. Use the @property decorator for the getter method.3. Use the @balance.setter decorator for the setter method and include validation.Answer:
class BankAccount: def __init__(self, balance): self._balance = balance @property def balance(self): return self._balance @balance.setter def balance(self, value): if value < 0: raise ValueError("Balance cannot be negative") self._balance = value
Why it works: The getter method provides access to the balance, and the setter method validates the input to maintain the integrity of the balance attribute.
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.