创建您自己的自定义容器怎么样?在class Empty(object):
def __init__(self, **kwargs):
object.__setattr__(self, '_obj', kwargs)
def __getattribute__(self, name):
obj = object.__getattribute__(self, '_obj')
try:
return obj[name]
except KeyError:
cls_name = object.__getattribute__(self, '__class__').__name__
raise AttributeError(
"'%(cls_name)s' object has no attribute '%(name)s'" % locals())
def __setattr__(self, name, val):
obj = object.__getattribute__(self, '_obj')
obj[name] = val
def __getitem__(self, key):
return getattr(self, key)
def __setitem__(self, key, val):
return setattr(self, key, val)
用法:
^{pr2}$
它将把所有内容存储在_obj字典下,这样您就可以获得一个与实际实例属性不冲突的干净空间。在