将
__iter__而不是类本身添加到元类中(假设Python 2.x):
class Foo(object): bar = "bar" baz = 1 class __metaclass__(type): def __iter__(self): for attr in dir(self): if not attr.startswith("__"): yield attr
对于Python 3.x,请使用
class metaFoo(type): def __iter__(self): for attr in dir(self): if not attr.startswith("__"): yield attrclass Foo(metaclass=metaFoo): bar = "bar" baz = 1
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)