在更新python中的'constant'属性时引发异常
问题内容:
由于python没有常量的概念,如果更新了’constant’属性,是否有可能引发异常?怎么样?
class MyClass():
CLASS_CONSTANT = 'This is a constant'
var = 'This is a not a constant, can be updated'
#this should raise an exception
MyClass.CLASS_CONSTANT = 'No, this cannot be updated, will raise an exception'
#this should not raise an exception
MyClass.var = 'updating this is fine'
#this also should raise an exception
MyClass().CLASS_CONSTANT = 'No, this cannot be updated, will raise an exception'
#this should not raise an exception
MyClass().var = 'updating this is fine'
任何将CLASS_CONSTANT更改为类属性或实例属性的尝试都应引发异常。
将var更改为类属性或实例属性不应引发异常。
问题答案:
__setattr__
在每个类中进行自定义(例如,@
ainab的答案指向的我的旧食谱中的示例以及其他答案),只能停止分配给INSTANCE属性而不是CLASS属性。因此,所有现有答案都不会真正满足您所说的要求。
如果您要求的IS实际上正是您想要的,则可以使用自定义元类和描述符的某种混合,例如:
class const(object):
def __init__(self, val): self.val = val
def __get__(self, *_): return self.val
def __set__(self, *_): raise TypeError("Can't reset const!")
class mcl(type):
def __init__(cls, *a, **k):
mkl = cls.__class__
class spec(mkl): pass
for n, v in vars(cls).items():
if isinstance(v, const):
setattr(spec, n, v)
spec.__name__ = mkl.__name__
cls.__class__ = spec
class with_const:
__metaclass__ = mcl
class foo(with_const):
CLASS_CONSTANT = const('this is a constant')
print foo().CLASS_CONSTANT
print foo.CLASS_CONSTANT
foo.CLASS_CONSTANT = 'Oops!'
print foo.CLASS_CONSTANT
这是相当高级的内容,因此__setattr__
,尽管它不满足您的要求,但您可能更喜欢其他答案中建议的更简单的方法(即,为了获得简单性,您可以合理地选择弱化您的要求;-)。但是这里的技术可能仍然很有趣:自定义描述符类型const
是另一种方式(阻止__setattr__
在每个需要某些常量的类中进行重写,并且使所有属性变为常量,而不是选择和选择…)来阻止分配给实例属性;其余的代码是关于一个自定义元类的,该自定义元类自身创建了唯一的每个类的子元类,以便最大程度地利用所述自定义描述符,并实现您明确要求的确切功能。