Detailed use of Python @property

1. Role

The method of converting class to class attributes can be used direct for getting a property value or attribute assignment

2. implementation

Used to implement property class, may be implemented using the decorative property, the two are essentially the same. Decorator implemented in most cases.

class Student (Object): 
    the @Property 
    DEF Score (Self):
         return self._score 

    @ score.setter 
    DEF Score (Self, value):
         IF  not isinstance (value, int):
             The raise ValueError ( ' score must be an integer ' )
         IF value <0 or value> 100 :
             The raise a ValueError ( ' must score between 0-100 ' ) 
        self._score = value

student = Student()
student.score = 65
print(student.score)



65

score increased (on) Method @property decorator is equivalent to score = property (fget = score), the score assigned to the property instance.

So, after the score was decorated, this is not an instance method score, but examples of the score property.

Guess you like

Origin www.cnblogs.com/bob-coder/p/11532718.html