首页 > 其他 > 详细

元类编程--property动态属性

时间:2018-04-18 18:00:38      阅读:173      评论:0      收藏:0      [点我收藏+]
from datetime import date, datetime
class User:
    def __init__(self, name, birthday):
        self.name = name
        self.birthday = birthday
        self._age = 0

    # def get_age(self):
    #     return datetime.now().year - self.birthday.year

    @property #动态属性
    def age(self): #属性描述符,get方法
        return datetime.now().year - self.birthday.year

    @age.setter
    def age(self, value):
        self._age = value

if __name__ == "__main__":
    user = User("bobby", date(year=1987, month=1, day=1))
    user.age = 30
    print (user._age)
    print(user.age)

技术分享图片

 

元类编程--property动态属性

原文:https://www.cnblogs.com/Erick-L/p/8876276.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!