Practical Use Cases
class Employee:
def __init__(self, salary):
self._salary = salary
@property
def salary(self):
return self._salary
@salary.setter
def salary(self, value):
if not isinstance(value, (int, float)):
raise TypeError("Salary must be a number")
if value < 0:
raise ValueError("Salary cannot be negative")
self._salary = round(value, 2)
Lazy Loading and Caching
class DatabaseConnection:
def __init__(self, connection_string):
self._connection_string = connection_string
self._connection = None
@property
def connection(self):
if self._connection is None:
self._connection = self._establish_connection()
return self._connection
def _establish_connection(self):
## Simulate expensive connection process
return f"Connected to {self._connection_string}"
Read-Only Attributes
class ImmutableConfig:
def __init__(self, config_dict):
self._config = config_dict
@property
def database_host(self):
return self._config.get('database_host')
@property
def database_port(self):
return self._config.get('database_port')
Use Case Scenarios
Scenario |
Property Benefit |
Example |
Input Validation |
Prevent invalid data |
Age verification |
Computed Values |
Dynamic calculations |
Area of geometric shapes |
Access Control |
Restrict direct modifications |
Sensitive data protection |
Logging and Monitoring
class SensorData:
def __init__(self):
self._temperature = 0
@property
def temperature(self):
return self._temperature
@temperature.setter
def temperature(self, value):
print(f"Temperature changed: {self._temperature} -> {value}")
self._temperature = value
Property Dependency Management
class Rectangle:
def __init__(self, width, height):
self._width = width
self._height = height
@property
def width(self):
return self._width
@width.setter
def width(self, value):
self._width = value
## Trigger potential recalculations
self._update_derived_properties()
@property
def area(self):
return self._width * self._height
def _update_derived_properties(self):
## Additional logic for dependent properties
pass
Property Creation Workflow
graph TD
A[Identify Attribute Need] --> B{Requires Custom Logic?}
B -->|Yes| C[Define Property Methods]
B -->|No| D[Use Standard Attribute]
C --> E[Implement Getter/Setter]
E --> F[Add Validation/Transformation]
LabEx Recommendation
At LabEx, we emphasize using dynamic properties to create more intelligent and self-managing classes that encapsulate complex logic while maintaining clean, readable code.
Advanced Composition
class User:
def __init__(self, first_name, last_name):
self._first_name = first_name
self._last_name = last_name
@property
def full_name(self):
return f"{self._first_name} {self._last_name}"
@full_name.setter
def full_name(self, name):
self._first_name, self._last_name = name.split(' ', 1)