Use of @ property in Python

Time:2022-1-2
catalogue
  • 1. Several concepts
  • 2. For example
  • 3. Problem solving
  • 4. Another way

Usually, when we need to privatize sensitive properties of objects or properties that we don’t want direct external access, but sometimes we need to modify these private properties, what should we do?

1. Several concepts

  • _ A (single underscore in front). This attribute only represents the agreed private, not the real private.
  • __ A (preceded by double underline). This attribute represents private and cannot be accessed externally.
  • _ a_ (double underlined before and after), this attribute identifies the system attribute. (optional)
  • a_ (single underline after), this attribute is to avoid conflicts with reserved keywords. (optional)

2. For example

Define a class:


class Student(object):

    _sex='male'

    __age=0

Execution: (private properties cannot be accessed externally)


>>> stu = Student()
>>> stu._sex
'male'
>>> stu.__age
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Student' object has no attribute '__age'
>>>

3. Problem solving

From the above class, we can see that private properties cannot be accessed in class instances. What should we do? When we need private properties of a class__ We can define get when querying and modifying age_ Age and set_ To achieve.


class Student(object):

    _sex='male'

    __age=0

    def get_age(self):
        return self.__age

    def set_age(self,age):
        self.__age = age

Execution:


>>> stu = Student()
>>> stu.get_age()   
0
>>> stu.set_age(18) 
>>> stu.get_age()   
18
>>>

4. Another way

However, the above method is slightly complex, and it is not applicable in classes with more private attributes, so we hope to find a simpler way to solve this problem, such as converting this private attribute into another attribute. The good news is that Python has helped us implement @ property.


class Student(object):

    _sex='male'

    __age=0

    def get_age(self):
        return self.__age

    def set_age(self,age):
        self.__age = age
    
    @property
    def age(self):
        return self.__age

Execution:


>>> from payhlib import Student
>>> s = Student()
>>> s.age
0
>>> s.set_age(19)
>>> s.age
19
>>

Up there we will__ The private attribute of age is converted to the attribute of age. You may think that since the private attribute is converted to the attribute, can we modify it directly? The answer is no, because although property will__ Age is converted to attribute, but it does not have setter function, so we need to add it.


>>> from payhlib import Student
>>> s = Student()
>>> s.age  
0
>>> s.age=20
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: can't set attribute
>>>

Add setter method


class Student(object):

    _sex='male'

    __age=0

    def get_age(self):
        return self.__age

    def set_age(self,age):
        self.__age = age
    
    @property
    def age(self):
        return self.__age
    
    @age.setter
    def age(self,value):
        self.__age=value

Execution:


>>> from payhlib import Student
>>> s = Student()
>>> s.age
0
>>> s.age=20
>>> s.age    
20
>>>

Now @ peoperty has been shared. You can check the source code for research on its implementation principle. For more information about @ property of python, please search the previous articles of developeppaer or continue to browse the relevant articles below. I hope you will support developeppaer in the future!