【发布时间】:2020-10-26 22:53:46
【问题描述】:
问题
我希望能够迭代一个类型而不实例化它,类似于枚举。
class Foo:
"""Class I want to iterate over without instantiating."""
ALLOWED_VALUES = (1, 2, 3)
# I want iterating over `Foo` to be equivalent to iterating over `Foo.ALLOWED_VALUES`
for val_from_tuple, val_from_foo in zip(Foo.ALLOWED_VALUES, Foo):
assert val_from_tuple == val_from_foo
这种行为可以通过枚举实现,但前提是 ALLOWED_VALUES 是有效的 Python 名称。我想在没有这个限制的情况下拥有相同的迭代行为。
我尝试了什么
我尝试在Foo 上将__iter__() 实现为staticmethod,这样就不需要Foo 的实例来获取Iterator。这允许我迭代 Foo.__iter__(),但 iter(Foo) 会引发错误。这似乎是因为iter(Foo) 在type 上寻找__iter__ 方法,而不是在Foo 上(因为Foo 是type 对象)。
class Foo:
"""Class I want to iterate over without instantiating."""
ALLOWED_VALUES = (1, 2, 3)
@staticmethod
def __iter__():
return Foo.ALLOWED_VALUES
# This works, but isn't what I want because it involves calling `__iter__()` explicitly.
for val in Foo.__iter__():
print(val)
# This raises an error:
# `TypeError: 'type' object is not iterable`
for val in Foo:
print(val)
【问题讨论】:
标签: python class iterator static-methods magic-methods