added sidebar about special methods

This commit is contained in:
Mark Pilgrim
2009-07-13 22:12:11 -04:00
parent df99f75902
commit 959de083ea
2 changed files with 13 additions and 1 deletions
Regular → Executable
+9 -1
View File
@@ -47,7 +47,7 @@ Classname Legend
.note = "note/caution/important" = indented block for tips/gotchas/language comparisons
.baa = "best available ampersand" = wrapper block for ampersands
.ots = "on the side" = an aside that is set in normal type (as opposed to a big blue pullquote)
Acknowledgements & Inspirations
@@ -283,6 +283,14 @@ aside {
-webkit-border-radius: 1em;
border-radius: 1em;
}
aside.ots {
font-style: normal;
font-size: medium;
line-height: 1.75;
}
aside a {
color: #fff !important;
}
/* previous/next navigation links */
+4
View File
@@ -158,6 +158,10 @@ body{counter-reset:h1 6}
<p><em>Now</em> you&#8217;re ready to learn how to build an iterator. An iterator is just a class that defines an <code>__iter__()</code> method.
<aside class=ots>
All three of these class methods, <code>__init__</code>, <code>__iter__</code>, and <code>__next__</code>, begin and end with a pair of underscore (<code>_</code>) characters. Why is that? There&#8217;s nothing magical about it, but it usually indicates that these are &#8220;<dfn>special methods</dfn>.&#8221; The only thing &#8220;special&#8221; about special methods is that they aren&#8217;t called directly; Python calls them when you use some other syntax on the class or an instance of the class. <a href=special-method-names.html>More about special methods</a>.
</aside>
<p class=d>[<a href=examples/fibonacci2.py>download <code>fibonacci2.py</code></a>]
<pre><code class=pp><a>class Fib: <span class=u>&#x2460;</span></a>
<a> def __init__(self, max): <span class=u>&#x2461;</span></a>