至少,你可以打个电话
property
作为一个函数,而不是将其用作装饰器。同时,可以将基础值存储在列表或dict中,而不是作为单独的属性。
class MyClass(object):
def __init__(self):
self._values = [...]
a = property(lambda self: self._values[0])
b = property(lambda self: self._values[1])
# etc
但是,只读属性实际上不需要将其值存储在实例dict中;只需直接在getter中硬编码该值即可:
class MyClass(object):
a = property(lambda self: "foo")
b = property(lambda self: "bar")
然后将对属性的调用包装到另一个函数中:)
def constant(value):
def _(self):
return value
return property(_)
class MyClass(object):
a = constant("foo")
b = constant("bar")
下面是一个纯python只读属性,它以
https://docs.python.org/3/howto/descriptor.html#properties
以下内容:
class Constant(object):
def __init__(self, value)
def _(self):
return value
self.fget = _
def __get__(self, obj, objtype=None):
if obj is None:
return self
return self.fget(obj)
这可能比子类化更简单
财产
和超越
__set__
和
__del__
让他们“无动于衷”。但是,我更喜欢用包装器包装常规属性的想法。