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 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.
To answer your questions:

How does the mechanism behind in works?

It is the exact same mechanism as used for ordinary for loops, as others have already noted.

Which functions\methods within X does it call?

As noted in a comment below, it calls `iter(X)` to get an iterator. If `X` has a method function `__iter__()` defined, this will be called to return an iterator; otherwise, if `X` defines `__getitem__()`, this will be called repeatedly to iterate over `X`. See the Python documentation for `iter()` here: <http://docs.python.org/library/functions.html#iter>

If X can comply to more than one method, what's the precedence?

I'm not sure what your question is here, exactly, but Python has standard rules for how it resolves method names, and they are followed here. Here is a discussion of this:

[Method Resolution Order (MRO) in new style Python classes](https://stackoverflow.com/questions/1848474/method-resolution-order-mro-in-new-style-python-classes)

How to write an efficient X, so that list comprehension will be quick?

I suggest you read up more on iterators and generators in Python. One easy way to make any class support iteration is to make a generator function for **iter**(). Here is a discussion of generators:

<http://linuxgazette.net/100/pramode.html>