python: are property fields being cached automatically?

后端 未结 8 726
离开以前
离开以前 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:00

    I've had to look it up, since I had this same question.

    The functools package from the standard library will be getting a cached_property decorator as well. Unfortunately, it's only available from Python 3.8 (as of time of this post, it's 3.8a0). The alternative to waiting is to use a custom one, such as this one as mentioned by 0xc0de) or Django's, for now, then switch later:

    from django.utils.functional import cached_property
    # from functools import cached_property # Only 3.8+ :(
    

提交回复
热议问题