Junior — Middle
How is the method of getting a property value declared in object-oriented programming?
sobes.tech AI
Answer from AI
In Python, the @property decorator is commonly used to declare a getter method for a property. It allows you to access the method as if it were an attribute of the object, providing convenient and safe access to internal data.
Example:
class Person:
def __init__(self, name):
self._name = name
@property
def name(self):
return self._name
p = Person("Ivan")
print(p.name) # accesses as an attribute, calls the name method
Thus, a method with @property acts as a getter for the name property.