python: are property fields being cached automatically?

后端 未结 8 713
离开以前
离开以前 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条回答
  •  Happy的楠姐
    2020-12-14 16:02

    To anyone who might be reading this in 2020, this functionality is now available in the funcutils module as part of the standard library as of Python 3.8.

    https://docs.python.org/dev/library/functools.html#functools.cached_property

    Important to note, classes that define their own __dict__ (or do not define one at all) or use __slots__ might not work as expected. For example, NamedTuple and metaclasses.

提交回复
热议问题