Question: When using list comprehension or the `in` keyword in a for loop context, i.e:

```
for o in X:
    do_something_with(o)

```

or

```
l=[o for o in X]

```

* How does the mechanism behind `in` works?
* Which functions\methods within `X` does it call?
* If `X` can comply to more than one method, what's the precedence?
* How to write an efficient `X`, so that list comprehension will be quick?

Answer: 
`X` must be iterable. It must implement `__iter__()` which returns an iterator object; the iterator object must implement `next()`, which returns next item every time it is called or raises a `StopIteration` if there's no next item.

Lists, tuples and generators are all iterable.

Note that the plain `for` operator uses the same mechanism.
Maybe this helps (tutorial <http://docs.python.org/tutorial/classes.html> Section 9.9):

> 
> Behind the scenes, the for statement
>  calls iter() on the container object.
>  The function returns an iterator
>  object that defines the method next()
>  which accesses elements in the
>  container one at a time. When there
>  are no more elements, next() raises a
>  StopIteration exception which tells
>  the for loop to terminate.
> 
> 
>