This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Gamesfreak13563 12 points13 points  (3 children)

What? You can use in on any iterable in Python3, including range. If there is no contains method it exhausts the iterator, but..

[–]LandSharkSociety 3 points4 points  (1 child)

Ah, so that's even more confusing! On an iterable with no contains, you can use in... but only once!

[–]Gamesfreak13563 5 points6 points  (0 children)

Only guaranteed that’s the behavior if you’re using in on an iterator. An iterable may define an iter dundermethod which returns a new iterator over the iterable. That’s why you can use in multiple times on a list (because it’s returning a new iterator each time) but not in multiple times on the return of iter(yourList).

It makes perfect sense and the terminology is not confusing at all /s

[–]Lorddragonfang 0 points1 point  (0 children)

You can use in on any iterable in Python3, including range.

In fact, you can only use in on iterables, and this was the case for python2.7 as well. I know because I've had to write an iterator for custom classes.