Skip to content

Commit

Permalink
Clarification on __reversed__ method on sequences.
Browse files Browse the repository at this point in the history
  • Loading branch information
petrushev committed Dec 6, 2012
1 parent 103e2bb commit b002ea0
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions magicmethods.html
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ <h4>The magic behind containers</h4>
<dt><code>__iter__(self)</code></dt>
<dd>Should return an iterator for the container. Iterators are returned in a number of contexts, most notably by the <code>iter()</code> built in function and when a container is looped over using the form <code>for x in container:</code>. Iterators are their own objects, and they also must define an <code>__iter__</code> method that returns <code>self</code>.</dd>
<dt><code>__reversed__(self)</code></dt>
<dd>Called to implement behavior for the <code>reversed()</code> built in function. Should return a reversed version of the list.</dd>
<dd>Called to implement behavior for the <code>reversed()</code> built in function. Should return a reversed version of the sequence. Implement this only if the sequence class is ordered, like list of tuple.</dd>
<dt><code>__contains__(self, item)</code></dt>
<dd><code>__contains__</code> defines behavior for membership tests using <code>in</code> and <code>not in</code>. Why isn't this part of a sequence protocol, you ask? Because when <code>__contains__</code> isn't defined, Python just iterates over the sequence and returns <code>True</code> if it comes across the item it's looking for.</dd>
<dt><code>__missing__(self, key)</code></dt>
Expand Down Expand Up @@ -401,7 +401,7 @@ <h4>An example</h4>
<span class="k">return</span> <span class="nb">iter</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">values</span><span class="p">)</span>

<span class="k">def</span> <span class="nf">__reversed__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="nb">reversed</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">values</span><span class="p">)</span>
<span class="k">return</span> <span class="n">FunctionalList</span><span class="p">(</span><span class="nb">reversed</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">values</span><span class="p">))</span>

<span class="k">def</span> <span class="nf">append</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">values</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
Expand Down
4 changes: 2 additions & 2 deletions magicmethods.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ Without any more wait, here are the magic methods that containers use:
: Should return an iterator for the container. Iterators are returned in a number of contexts, most notably by the `iter()` built in function and when a container is looped over using the form `for x in container:`. Iterators are their own objects, and they also must define an `__iter__` method that returns `self`.

`__reversed__(self)`
: Called to implement behavior for the `reversed()` built in function. Should return a reversed version of the list.
: Called to implement behavior for the `reversed()` built in function. Should return a reversed version of the sequence. Implement this only if the sequence class is ordered, like list of tuple.

`__contains__(self, item)`
: `__contains__` defines behavior for membership tests using `in` and `not in`. Why isn't this part of a sequence protocol, you ask? Because when `__contains__` isn't defined, Python just iterates over the sequence and returns `True` if it comes across the item it's looking for.
Expand Down Expand Up @@ -491,7 +491,7 @@ For our example, let's look at a list that implements some functional constructs
return iter(self.values)

def __reversed__(self):
return reversed(self.values)
return FunctionalList(reversed(self.values))

def append(self, value):
self.values.append(value)
Expand Down

0 comments on commit b002ea0

Please sign in to comment.