python类常量属性,有关更新Python中的'常量'属性引发异常

As python does not have concept of constants, would it be possible to raise an exception if an 'constant' attribute is updated? How?

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'

Any attempt to change CLASS_CONSTANT as a class attribute or as an instance attribute should raise an exception.

Changing var as a class attribute or as an instance attribute should not raise an exception.

解决方案

Customizing __setattr__ in every class (e.g. as exemplified in my old recipe that @ainab's answer is pointing to, and other answers), only works to stop assignment to INSTANCE attributes and not to CLASS attributes. So, none of the existing answers would actually satisfy your requirement as stated.

If what you asked for IS actually exactly what you want, you could resort to some mix of custom metaclasses and descriptors, such as:

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

This is pretty advanced stuff, so you might prefer the simpler __setattr__ approach suggested in other answers, despite it NOT meeting your requirements as stated (i.e., you might reasonably choose to weaken your requirements in order to gain simplicity;-). But the techniques here might still be interesting: the custom descriptor type const is another way (IMHO far nicer than overriding __setattr__ in each and every class that needs some constants AND making all attributes constants rather than picking and choosing...) to block assignment to an instance attribute; the rest of the code is about a custom metaclass creating unique per-class sub-metaclasses of itself, in order to exploit said custom descriptor to the fullest and achieving the exact functionality you specifically asked for.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值