Python’s built-in @property decorator can make a class’s method-call like property-call. That is to say, the method call of foo.func() can be changed into foo.func. Besides @property decorator, there is also a built-in function property() can do the same thing. This article will tell you how to use @property decorator and property() function to achieve this.
1. @Property Decorator.
- Add the @property decorator to the normal method. This is equivalent to a get method that gets the value. After this the code like “result = obj.age” will execute this method, This method has only one self argument.
@property def age(self): return self.__age
- Write a method with the same name and add @xxx.setter decorator (xxx means the same name as above method). This is equivalent to write a set method to set the age value. This function will be executed when calling code like “obj.age =…“.
@age.setter def age(self, age): if isinstance(age, int): self.__age = age else: raise ValueError
- Write another method with the same name and add the @xxx.deleter decorator. This function will be executed when run python code “del obj.age“.
@age.deleter def age(self): print("delete age value.")
- Below is the full example source code.
class Employee: def __init__(self, name, age): print("*** Initialize Employee object.***") self.__name = name self.__age = age @property def age(self): print("***Get age property value.***") return self.__age @age.setter def age(self, age): print("***Set age property value.***") if isinstance(age, int): self.__age = age else: raise ValueError @age.deleter def age(self): self.__age = 0 print("***Delete age property value.***") if __name__ == '__main__': emp = Employee("tom", 88) print(emp.age) emp.age = 99 print("emp.age: ", emp.age) del emp.age print("emp.age: ", emp.age)
- Below is above code execution output.
*** Initialize Employee object.*** ***Get age property value.*** 88 ***Set age property value.*** ***Get age property value.*** emp.age: 99 ***Delete age property value.*** ***Get age property value.*** emp.age: 0
2. Python Property Function.
In addition to use @property decorator to make a method behave like a property, the property() function in Python’s built-in builtins module gives us a second way to make class function behave like property. A class method is changed as a property by the statement age = property (get_age_function, set_age_function, del_age_function, “age property documentation”). The effect is the same as that of the @property decorator. So you should define three functions in the class first then call above statement to build the age property. Below is an example.
class Department: def __init__(self, name): print("*** Initialize Department object.***") self.__name = name # First define three method for property get, set, delete operation. def get_name(self): print("***Get name property value.***") return self.__name def set_name(self, name): print("***Set name property value.***") self.__name = name def delete_name(self): self.__name = "" print("***Delete name property value.***") # Now use property() function to build a property name. The last parameter is the name property documentation. name = property(get_name, set_name, delete_name, "This is department name property.") if __name__ == '__main__': dept = Department("Dev") print(dept.name) dept.name = "QA" print(dept.name) del dept.name print(dept.name) # Print the Department class's name property documentation. print(Department.name.__doc__)
Below is above example execution output.
*** Initialize Department object.*** ***Get name property value.*** Dev ***Set name property value.*** ***Get name property value.*** QA ***Delete name property value.*** ***Get name property value.*** This is department name property.