pythonic的方式是使用
dict:
>>> foo = dict(x=1, y=2)>>> bar = dict(y=2, x=1)>>> foo == barTrue
满足您的所有要求,除了您仍然需要
foo['x']代替
foo.x。
如果有问题,您可以轻松定义一个类,例如:
class Bunch(object): def __init__(self, **kwds): self.__dict__.update(kwds) def __eq__(self, other): return self.__dict__ == other.__dict__
或者,一个不错的矮个子
class Bunch(dict): __getattr__, __setattr__ = dict.get, dict.__setitem__
(但是请注意,第二点有问题,正如Alex在评论中指出的那样!)



