Python Tutorial
Python Variable
Python Operators
Python Sequence
Python String
Python Flow Control
Python Functions
Python Class and Object
Python Class Members (properties and methods)
Python Exception Handling
Python Modules
Python File Operations (I/O)
The @property
decorator in Python is used to create read-only properties for an object. Properties are a way to access and modify attributes of a class using getter and setter methods, making it possible to add extra functionality or constraints to an attribute without directly accessing it.
In this tutorial, we will explore the usage of the @property
decorator in Python to create and manage properties in a class.
@property
to create a getter method:A getter method is used to retrieve the value of an attribute. To create a getter method, define a method in the class that returns the value of the attribute, and use the @property
decorator to mark it as a getter method.
class Circle: def __init__(self, radius): self._radius = radius @property def radius(self): return self._radius circle = Circle(5) print(circle.radius) # Output: 5
In this example, the radius
method returns the value of the _radius
attribute, and the @property
decorator is used to mark it as a getter method for the radius
property.
@<attribute>.setter
decorator to create a setter method:A setter method is used to modify the value of an attribute. To create a setter method, define a method in the class that sets the value of the attribute, and use the @<attribute>.setter
decorator to mark it as a setter method.
class Circle: def __init__(self, radius): self._radius = radius @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 circle = Circle(5) circle.radius = 10 # Set the radius using the setter method print(circle.radius) # Output: 10 try: circle.radius = -5 # Raises ValueError except ValueError as e: print(e) # Output: Radius cannot be negative
In this example, the radius
method sets the value of the _radius
attribute after checking if the value is non-negative, and the @radius.setter
decorator is used to mark it as a setter method for the radius
property.
@<attribute>.deleter
decorator to create a deleter method (optional):A deleter method is used to delete an attribute or perform cleanup before the attribute is deleted. To create a deleter method, define a method in the class that deletes the attribute or performs any necessary cleanup, and use the @<attribute>.deleter
decorator to mark it as a deleter method.
class Circle: def __init__(self, radius): self._radius = radius @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 @radius.deleter def radius(self): print("Deleting radius") del self._radius circle = Circle(5) del circle.radius # Calls the deleter method and deletes the _radius attribute
In this example, the radius
method with the @radius.deleter
decorator is used to perform cleanup before the _radius
attribute is deleted.
In conclusion, the @property
decorator in Python is used to create and manage properties in a class.
Creating read-only properties with @property
:
@property
decorator allows you to create read-only properties by defining a getter method without a corresponding setter method.class ReadOnlyPropertyExample: def __init__(self): self._value = 42 @property def value(self): return self._value obj = ReadOnlyPropertyExample() print(obj.value) # Accessing the read-only property
Getter methods and @property
in Python:
@property
decorator is often used with getter methods to create properties with customized behavior.class Circle: def __init__(self, radius): self._radius = radius @property def radius(self): return self._radius my_circle = Circle(5) print(my_circle.radius) # Accessing the property using the getter method
Setter methods and @property
in Python:
@property
decorator can be paired with a setter method to create properties with controlled assignment behavior.class Circle: def __init__(self, radius): self._radius = radius @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 my_circle = Circle(5) print(my_circle.radius) # Accessing the property using the getter method my_circle.radius = 10 # Assigning a new value using the setter method
Computed properties and @property
decorator:
@property
decorator can be used to create computed properties by defining only a getter method.class Rectangle: def __init__(self, width, height): self._width = width self._height = height @property def area(self): return self._width * self._height my_rect = Rectangle(4, 5) print(my_rect.area) # Computed property
Using @property
with classes and instances in Python:
@property
decorator can be applied at both the class level and the instance level, allowing flexibility in property definition.class ClassPropertyExample: _class_variable = "Class Variable" @property def instance_variable(self): return self._class_variable instance1 = ClassPropertyExample() instance2 = ClassPropertyExample() print(instance1.instance_variable) # Accessing the read-only property
Common use cases and scenarios for @property
:
@property
is commonly used when you want to provide controlled access to class attributes, define computed properties, or create read-only properties.class TemperatureConverter: def __init__(self, celsius): self._celsius = celsius @property def celsius(self): return self._celsius @property def fahrenheit(self): return (self._celsius * 9/5) + 32 temp_obj = TemperatureConverter(25) print(temp_obj.celsius) # Accessing celsius property print(temp_obj.fahrenheit) # Accessing computed fahrenheit property
Property inheritance and super()
with @property
in Python:
@property
can be inherited, and the super()
function can be used to call the property methods of the superclass.class ParentClass: def __init__(self): self._x = 42 @property def x(self): return self._x class ChildClass(ParentClass): @property def x(self): return super().x * 2 child_obj = ChildClass() print(child_obj.x) # Calls the overridden property in ChildClass