@property attribute decorator

After the name suggests, @ property is a property associated with decorators, used it, and values ​​have become simple assignment

 

from datetime import date, datetime


class User:
    def __init__(self, name, birthday):
        self.name = name
        self.birthday = birthday
        self._age = 0

    # 相当于get操作
    @property
    def age(self):
        # return datetime.now().year - self.birthday.year
        return self._age

    # 相当于set操作
    @age.setter
    def age(self, value):
        self._age = value


if __name__== ' __main__ ' : 
    user = the User ( ' Lucy ' , DATE (= 2000 year, month The =. 1, Day 2 = ))
     Print (user.age)   # If no decorative property on age method, this is not a 

    user .age = 66   # assignment operator 
    Print (user.age)   # 66 value operations

Guess you like

Origin www.cnblogs.com/z-qinfeng/p/12041426.html