我正在使用束类将dict转换为对象.
class Bunch(object):
""" Transform a dict to an object """
def __init__(self, kwargs):
self.__dict__.update(kwargs)
问题是,我的名字中有一个圆点({‘test.this’:True}).
所以当我打电话时:
spam = Bunch({'test.this':True})
dir(spam)
我有一个特点:
['__class__',
'__delattr__',
...
'__weakref__',
'test.this']
但我无法访问它:
print(spam.test.this)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
in ()
----> 1 print(spam.test.this)
AttributeError: 'Bunch' object has no attribute 'test'
我有一个AttributeError.
我该如何访问此属性?