@cached_property

from django.utils.functional import cached_property
import datetime

class User(object):
    birth_year = 1988

    @cached_property
    def age(self):
        return datetime.date.today().year - self.birth_year

if __name__ == '__main__':
    u = User()
    print(u.age)
    print(u.age)
  只执行一次age方法,第二次用缓存
  #结果
  32
  32

  

posted @ 2020-06-15 15:45  Fmaj-7  阅读(198)  评论(0编辑  收藏  举报