如何让python中的类支持__getitem__,但不允许迭代?

3 投票
2 回答
2029 浏览
提问于 2025-04-15 11:55

我想定义一个类,这个类可以使用 __getitem__ 方法,但不允许被循环遍历。举个例子:

class B:
   def __getitem__(self, k):
      return k

cb = B()

for x in cb:
   print x

我可以在类 B 中添加什么,让 for x in cb: 这个循环出错呢?

2 个回答

2

从这个问题的回答中,我们可以看到,如果类中定义了__iter__,那么在使用的时候会先调用__iter__,再调用__getitem__。所以,只需要这样定义B:

class B:
   def __getitem__(self, k):
      return k

   def __iter__(self):
      raise Exception("This class is not iterable")

然后:

cb = B()
for x in cb: # this will throw an exception when __iter__ is called.
  print x
14

我觉得一个稍微更好的解决办法是抛出一个类型错误,而不是普通的异常(这通常发生在一个不可迭代的类上):

class A(object):
    # show what happens with a non-iterable class with no __getitem__
    pass

class B(object):
    def __getitem__(self, k):
        return k
    def __iter__(self):
        raise TypeError('%r object is not iterable'
                        % self.__class__.__name__)

测试:

>>> iter(A())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'A' object is not iterable
>>> iter(B())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "iter.py", line 9, in __iter__
    % self.__class__.__name__)
TypeError: 'B' object is not iterable

撰写回答