class Bunch:
def __init__(self, **kwds):
self.__dict__.update(kwds)
# that's it! Now, you can create a Bunch
# whenever you want to group a few variables:
point = Bunch(datum=y, squared=y*y, coord=x)
# and of course you can read/write the named
# attributes you just created, add others, del
# some of them, etc, etc:
if point.squared > threshold:
point.isok = 1
尝试使用getattr python魔术:
也许你所要寻找的,能做你想做的工作,比你想做的优雅得多,是:
Alex Martelli的Bunch Class。在
链接的配方页面中有其他可用的实现。在
您只是将变量self引用为wrap()中的某个对象。wrap()结束时,该变量被垃圾回收。在
您可以简单地将对象保存在包装器属性中以实现所需的效果
相关问题 更多 >
编程相关推荐