Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
The iteration story is even richer than we’ve seen thus far. Operator overloading is often layered: classes may provide specific methods, or more general alternatives used as fallback options. For example:
Comparisons in Python 2.6 use specific methods such as __lt__ for less than if present, or else
the general __cmp__. Python 3.0
uses only specific methods, not __cmp__, as discussed later in this
chapter.
Boolean tests similarly try a specific __bool__ first (to give an explicit
True/False result), and if it’s absent fall
back on the more general __len__ (a nonzero length means True). As we’ll also see later in this
chapter, Python 2.6 works the same but uses the name __nonzero__ instead of __bool__.