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()
function in Python is used to create and manage 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()
function in Python to create and manage properties in a class.
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, then use the property()
function to mark it as a getter method.
class Circle: def __init__(self, radius): self._radius = radius def get_radius(self): return self._radius # Create a property for the radius attribute using the getter method radius = property(get_radius) circle = Circle(5) print(circle.radius) # Output: 5
In this example, the get_radius()
method returns the value of the _radius
attribute, and the radius
property is created using the property()
function with the get_radius
method as its argument.
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, then use the property().setter
decorator to mark it as a setter method.
class Circle: def __init__(self, radius): self._radius = radius def get_radius(self): return self._radius def set_radius(self, value): if value < 0: raise ValueError("Radius cannot be negative") self._radius = value # Create a property for the radius attribute using the getter and setter methods radius = property(get_radius) radius = radius.setter(set_radius) 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 set_radius()
method sets the value of the _radius
attribute after checking if the value is non-negative, and the radius
property is updated with the setter method using the radius.setter
decorator.
An alternative, more concise way to create properties in Python is to use the @property
and @<attribute>.setter
decorators.
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 print(circle.radius) # Output: 10
In this example, the radius
property is created using the @property
decorator for the getter method, and the @radius.setter
decorator for the setter method.
In conclusion, the property()
function in Python is used to create and manage properties in a class. Properties are a way to access and modify attributes of a class using getter and setter methods, allowing you to add extra functionality or constraints to an attribute. The property()
function and the @property
and `
Defining properties in Python with property()
:
property()
function allows you to create properties for an object, providing a way to customize the behavior of attribute access.class MyClass: def __init__(self): self._x = 0 def get_x(self): return self._x def set_x(self, value): self._x = value x = property(get_x, set_x) obj = MyClass() obj.x = 42 # Calls set_x print(obj.x) # Calls get_x
Getter and setter methods with property()
in Python:
property()
function allows you to define getter and setter methods for an attribute, controlling how values are retrieved and assigned.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) # Calls getter my_circle.radius = 10 # Calls setter
Read-only properties using property()
in Python:
property()
.class ReadOnlyClass: def __init__(self): self._value = 42 @property def value(self): return self._value obj = ReadOnlyClass() print(obj.value) # Accessing the read-only property
Computed properties and property()
in Python:
property()
function to create computed properties, where the value is computed based on other attributes.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
Handling property deletion with deleter methods in Python:
property()
can also include a deleter method, which is called when the property is deleted using del
.class DeleterClass: def __init__(self): self._data = "Hello" @property def data(self): return self._data @data.deleter def data(self): print("Deleting data...") del self._data obj = DeleterClass() print(obj.data) del obj.data # Calls deleter method
Using property()
with classes and instances in Python:
property()
can be used both at the class level and the instance level, allowing you to define properties for all instances or specific instances.class ClassPropertyExample: _class_variable = "Class Variable" @property def instance_variable(self): return self._class_variable instance1 = ClassPropertyExample() instance2 = ClassPropertyExample() print(instance1.instance_variable) # Accessing instance variable
Property inheritance and super()
with property()
in Python:
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