I am Utpal Vishwas from Uttar Pradesh. Have completed my B. Tech. course from MNNIT campus Prayagraj in 2022. I have good knowledge of computer networking.
In Python, the @property decorator is used to define a method as a getter method for a class attribute. It allows you to access the attribute using the dot notation as if it were a regular property, while also allowing you to add custom logic to the attribute's getter method.
Here's an example of how the @property decorator works:
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):
if value <= 0:
raise ValueError("Width must be positive")
self._width = value
@property
def height(self):
return self._height
@height.setter
def height(self, value):
if value <= 0:
raise ValueError("Height must be positive")
self._height = value
@property
def area(self):
return self._width * self._height
In Python, a decorator encloses a function, adds a number of features, and then returns the result.
The @property decorator in Python is used to define a method as a read-only attribute
of a class. It allows us to access a method like an attribute, without the need to invoke it like a function.
Here's an example to illustrate the usage of @property decorator:
class Person:
def __init__(self, name, age):
self._name = name
self._age = age
@property
def age(self):
return self._age
@property
def name(self):
return self._name
@name.setter
def name(self, name):
self._name = name
Here's how the @property decorator works:
First, a method is defined inside a class, and the @property decorator is added before the method definition.
The @property decorator tells Python that the method is a getter method, which means that it will be used to retrieve the value of a private class variable.
The method is given a name that matches the name of the private variable it will be used to retrieve.
The private variable must be defined outside the method, usually at the beginning of the class definition, and must be prefixed with an underscore to indicate that it is a private variable.
Liked By
Write Answer
How the @property decorator work in Python?
Join MindStick Community
You have need login or register for voting of answers or question.
Aryan Kumar
19-Apr-2023In Python, the @property decorator is used to define a method as a getter method for a class attribute. It allows you to access the attribute using the dot notation as if it were a regular property, while also allowing you to add custom logic to the attribute's getter method.
Here's an example of how the @property decorator works:
Krishnapriya Rajeev
13-Apr-2023In Python, a decorator encloses a function, adds a number of features, and then returns the result.
The @property decorator in Python is used to define a method as a read-only attribute of a class. It allows us to access a method like an attribute, without the need to invoke it like a function.
Here's an example to illustrate the usage of @property decorator:
Here's how the @property decorator works: