I like to use a special way of crafting a dict
subclass which I found here. It looks like:
class Struct(dict):
"""Python Objects that act like Javascript Objects"""
def __init__(self, *args, **kwargs):
super(Struct, self).__init__(*args, **kwargs)
self.__dict__ = self
This object can be used this way:
o = Struct(x=10)
o.y = 20
o['z'] = 30
print o.x, o['y'], o.z
The different types of access can be used in a exchangeable way.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…