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.
`X` must be an iterable object, meaning it needs to have an `__iter__()` method.

So, to start a `for..in` loop, or a list comprehension, first `X`'s `__iter__()` method is called to obtain an iterator object; then that object's `next()` method is called for each iteration until `StopIteration` is raised, at which point the iteration stops.

I'm not sure what your third question means, and how to provide a meaningful answer to your fourth question except that your iterator should not construct the entire list in memory at once.