【发布时间】:2017-01-22 18:12:32
【问题描述】:
是否可以在一个类上拥有一个静态属性,该属性将被计算为一次性。我们的想法是能够这样做:
class Foo:
static_prop = Foo.one_off_static_method()
@staticmethod
def one_off_static_method():
return 'bar'
我也想过用__new__。
Class Foo:
def __new__(cls):
cls.static_prop = ... do everything here
但不确定这意味着什么。
【问题讨论】:
-
你想要这个懒惰的评估吗(它实际上是否足够昂贵并且有足够的可能永远不会被使用,所以懒惰的评估是有意义的)?
-
您是否尝试将静态方法设为属性?
-
您希望在首次使用时还是在类定义时计算它?
-
@user2357112 这很昂贵,打开读取文件并创建数据结构。
标签: python static-methods