0% found this document useful (0 votes)
57 views3 pages

@properety Decorator

The @property decorator in Python allows defining methods in a class that can be accessed like attributes. This includes adding validation, computation, and protection when getting or setting values. A getter method accessed with @property returns an attribute value, while a setter method accessed with @attribute.setter sets the attribute value and can include validation. In an example Circle class, radius and diameter properties are managed with getter and setter methods that validate values and maintain attribute interdependence. [/SUMMARY]

Uploaded by

Md Ahsan Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views3 pages

@properety Decorator

The @property decorator in Python allows defining methods in a class that can be accessed like attributes. This includes adding validation, computation, and protection when getting or setting values. A getter method accessed with @property returns an attribute value, while a setter method accessed with @attribute.setter sets the attribute value and can include validation. In an example Circle class, radius and diameter properties are managed with getter and setter methods that validate values and maintain attribute interdependence. [/SUMMARY]

Uploaded by

Md Ahsan Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

@property Decorator in Python

The @property decorator in Python is a built-in decorator used for creating and
managing attributes in an object-oriented manner. It allows you to use getters and
setters in order to add logic around getting and setting a value, rather than using
direct attribute access. This can be essential for ensuring encapsulation, which is
one of the key concepts of object-oriented programming.

Purpose of the @property Decorator


The purpose of the @property decorator is to define methods in a class that can be
accessed like attributes but with additional logic that you would want to execute
automatically when getting or setting a value. This includes:

Validation: Ensuring the value assigned to an attribute meets certain conditions.


Computation: Calculating the value of an attribute based on other attributes.
Protection: Preventing direct modification of a private attribute.
Getter Method
A getter method is used to access the value of an attribute safely. It is defined
by using the @property decorator above a method. The method's name is typically the
name of the attribute you want to access.

Setter Method
A setter method allows you to set the value of an attribute while incorporating any
necessary validation or transformation. It is defined with the same name as the
getter method but is decorated with @<attribute_name>.setter.

Example with Complex Problem


Consider a complex scenario where you have a Circle class, and you want to manage
the radius and diameter attributes with proper validation and inter-dependence:

python
Copy
import math

class Circle:
def __init__(self, radius):
self._radius = radius

@property
def radius(self):
"""Getter for radius."""
return self._radius

@radius.setter
def radius(self, value):
"""Setter for radius, with validation."""
if value < 0:
raise ValueError("Radius cannot be negative")
self._radius = value
self._diameter = value * 2 # Update the diameter as well

@property
def diameter(self):
"""Getter for diameter."""
return self._diameter

@diameter.setter
def diameter(self, value):
"""Setter for diameter, with validation and updating the radius."""
if value < 0:
raise ValueError("Diameter cannot be negative")
self._radius = value / 2 # Update the radius
self._diameter = value

@property
def area(self):
"""Getter for area, computed dynamically."""
return math.pi * self._radius ** 2

# Usage
circle = Circle(5)
print(circle.radius) # 5
print(circle.diameter) # 10
print(circle.area) # 78.53981633974483

circle.radius = 7 # Update the radius, the diameter will be updated


automatically
print(circle.diameter) # 14

try:
circle.radius = -10 # Attempting to set a negative radius
except ValueError as e:
print(e) # Radius cannot be negative
In this Circle class:

The radius property is managed by a getter and setter method that validates the new
radius value.
The diameter property is also managed by a getter and setter method. Setting the
diameter automatically updates the radius.
The area property is read-only and computed dynamically from the current radius.
There's no setter method for area, as it's dependent on the radius.
This is a complex example where the properties (radius, diameter, and area) are
interrelated. The @property decorator along with associated getter/setter methods
provides a way to encapsulate the attributes of the Circle class, maintaining the
integrity of the object's state, and automating the inter-attribute dependencies
and calculations.

Can you provide an example of using the @property decorator with a different class
and attributes?

GPT-4
GPT-4

Poe
Certainly! Let's consider a Temperature class that needs to handle temperatures in
both Celsius and Fahrenheit units. We would like to be able to set and get
temperatures in both units, but we'll store the value internally in only one unit
(Celsius). We'll use the @property decorator to create getters and setters for both
Celsius and Fahrenheit.

Here's how the Temperature class can be implemented:

python
Copy
class Temperature:
def __init__(self, celsius):
self._celsius = celsius
@property
def celsius(self):
"""Getter for Celsius temperature."""
return self._celsius

@celsius.setter
def celsius(self, value):
"""Setter for Celsius temperature. Also validates the temperature."""
if value < -273.15:
raise ValueError("Temperature cannot go below absolute zero!")
self._celsius = value

@property
def fahrenheit(self):
"""Getter for Fahrenheit temperature, converts from Celsius."""
return (self._celsius * 9/5) + 32

@fahrenheit.setter
def fahrenheit(self, value):
"""Setter for Fahrenheit temperature, converts to Celsius."""
self._celsius = (value - 32) * 5/9

# Usage
temp = Temperature(0) # 0 Celsius
print(temp.celsius) # 0
print(temp.fahrenheit) # 32 (Fahrenheit equivalent of 0 Celsius)

temp.fahrenheit = 212 # Setting the temperature to 212 Fahrenheit


print(temp.celsius) # 100 (Celsius equivalent of 212 Fahrenheit)
print(temp.fahrenheit) # 212

# Trying to set a temperature below absolute zero


try:
temp.celsius = -300
except ValueError as e:
print(e) # Temperature cannot go below absolute zero!
In this example, the Temperature class has two properties: celsius and fahrenheit.

The celsius property has both a getter and a setter. The setter includes a
validation check to ensure the temperature does not go below absolute zero in
Celsius.
The fahrenheit property also has both a getter and a setter. The getter performs
the conversion from Celsius to Fahrenheit, while the setter does the reverse
conversion from Fahrenheit to Celsius.
By using the @property decorator, we can neatly encapsulate the temperature
conversion logic within the class. This allows users of the Temperature class to
work with the object as if it had direct celsius and fahrenheit attributes, without
having to worry about the underlying conversion details.

You might also like