Is "... an object not iterable"? Error entering "x to y"?

Consider the following two classes:

class Foo0(object):                                                 
    pass

class Foo1(object):
    def __contains__(self, _):
        return False

None of them are iterable, which can be seen by trying for i in Foo1(): pass:

Traceback (most recent call last):
   File "stuff.py", line 11, in <module>
    for i in Foo1(): pass
TypeError: 'Foo1' object is not iterable

And vice versa, it is 3 in Foo1()valid (as it Foo1has __contains__), while 3 in Foo0()not:

Traceback (most recent call last):
  File "stuff.py", line 9, in <module>
    3 in Foo0()
TypeError: argument of type 'Foo0' is not iterable

. , Foo0 - Foo1 , . , @niemmi @tobias_k , __contains__, __iter__ , . , , __iter__, __contains__.

, ?

+4
1

__contains__, Python , , . Foo0 , .

UPDATE: Python :

, (), x y , y. (x). p >

, (), iter(), x y , - z x == z y. , .

, : getitem(), x y , , x == y [i], IndexError. ( - , ).

+7

All Articles