Caching class attributes in Python

后端 未结 9 1046
暖寄归人
暖寄归人 2020-11-30 22:05

I\'m writing a class in python and I have an attribute that will take a relatively long time to compute, so I only want to do it once. Also, it will not be

9条回答
  •  一整个雨季
    2020-11-30 22:25

    The most simple way of doing this would probably be to just write a method (instead of using an attribute) that wraps around the attribute (getter method). On the first call, this methods calculates, saves and returns the value; later it just returns the saved value.

提交回复
热议问题