python: are property fields being cached automatically?

后端 未结 8 716
离开以前
离开以前 2020-12-14 15:20

My question is are the following two pieces of code run the same by the interpreter:

class A(object):
  def __init__(self):
     self.__x = None

  @property         


        
8条回答
  •  一向
    一向 (楼主)
    2020-12-14 16:02

    Python 3.2 onwards offers a built-in decorator that you can use to create a LRU cache:

    @functools.lru_cache(maxsize=128, typed=False)

    Alternatively, if you're using Flask / Werkzeug, there's the @cached_property decorator.

    For Django, try from django.utils.functional import cached_property

提交回复
热议问题