How exactly python converts arbitrary object into list?
<h2>Documentation <a href="https://docs.python.org/3.5/library/stdtypes.html#typesseq-list" rel="nofollow">says</a>:</h2>

<blockquote>
  <p>The constructor builds a list whose items are the same and in the same
  order as iterable‘s items. iterable may be either a sequence, a
  container that supports iteration, or an iterator object. If iterable
  is already a list, a copy is made and returned, similar to
  iterable[:]...</p>
</blockquote>

<p>But if I have an object <code>a</code> of my class <code>A</code>, that implements <code>__iter__</code>, <code>__len__</code> and <code>__getitem__</code>, which interface is used by <code>list(a)</code> to iterate my object and what logic is behind this?</p>

<h2>My quick experimenting confuses me:</h2>

<pre class="lang-python prettyprint-override"><code>class A(object):
    def __iter__(self):
        print '__iter__ was called'
        return iter([1,2,3])
    def __len__(self):
        print '__len__ was called'
        return 3
    def __getitem__(self, index):
        print '__getitem(%i)__ was called' % index
        return index+1

a = A()
list(a)
</code></pre>

<p>Outputs</p>

<pre><code>__iter__ was called
__len__ was called
[1, 2, 3]
</code></pre>

<p><code>A.__iter__</code> was called first, ok. But why then <code>A.__len__</code> was called? And then why <code>A.__getitem__</code> was not called?</p>

<h2>Then I turned <code>__iter__</code> to a generator</h2>

<p><em>And this changed the order of magic method calls!</em></p>

<pre class="lang-python prettyprint-override"><code>class B(object):
    def __iter__(self):
        print '__iter__ was called'
        yield 1
        yield 2
        yield 3
    def __len__(self):
        print '__len__ was called'
        return 3
    def __getitem__(self, index):
        print '__getitem(%i)__ was called' % index
        return index+1      

b = B()
list(b)
</code></pre>

<p>Outputs</p>

<pre><code>__len__ was called
__iter__ was called
[1, 2, 3]
</code></pre>

<p>Why <code>B.__len__</code> was called first now? But why then <code>B.__getitem__</code> was not called, and conversion was done with <code>B.__iter__</code>?</p>

<p>And what confuses me most is why the order of calls of <code>__len__</code> and <code>__iter__</code> is different in cases of <code>A</code> and <code>B</code>?</p>