[Tutor] special attributes naming confusion

Dave davestechshop at gmail.com
Wed Jun 6 21:59:03 CEST 2012


On Wed, Jun 6, 2012 at 3:40 PM, Mark Lawrence <breamoreboy at yahoo.co.uk>wrote:

> On 06/06/2012 20:19, Dave wrote:
>
>> I was reading some tutorial material on creating iterators. It shows the
>> following example implementation of an iterator:
>>
>> class Reverse:
>>     """Iterator for looping over a sequence backwards."""
>>     def __init__(self, data):
>>         self.data = data
>>         self.index = len(data)
>>     def __iter__(self):
>>         return self
>>     def next(self):
>>         if self.index == 0:
>>             raise StopIteration
>>         self.index = self.index - 1
>>         return self.data[self.index]
>>
>>
>> My question is how was I supposed to kinow that the function I call using
>> the name iter() is implemented using the name __iter__()?
>>
>> Is there a rule that describes when I would implement an attribute name
>> with leading and trailing double underscores, and then call it without
>> those underscores? How many names like this exist in Python? Are these
>> special cases or is there a general rule that leading and trailing double
>> underscores get dropped when calling functions that were implemented with
>> these names? I'm trying to understand the big picture as far as how Python
>> works when it comes to this situation. Thanks.
>>
>>
> Try this to start with http://docs.python.org/**reference/datamodel.html#*
> *special-method-names<http://docs.python.org/reference/datamodel.html#special-method-names>.
> Note this is for Python 2.7.3, there may be differences in Python 3.x.
>
> --
>

Actually, I think I'm getting it now... as I read more of this page I see
that there is no single generalization. These are indeed all special cases.

But the documentation does appear to be incomplete. It leaves out the
mapping to the name or symbol that should be used to call the special
function in some cases. In particular, in the case of __iter(self), which
is one of the first ones I looked at, it doesn't mention that this is
usually called via iter(). It does mention how it would be called for
mapping containers, however (i.e., iterkeys()).
 object.__iter__(*self*)

This method is called when an iterator is required for a container. This
method should return a new iterator object that can iterate over all the
objects in the container. For mappings, it should iterate over the keys of
the container, and should also be made available as the method iterkeys().

But as I read more, I see that much of the documentation does mention how
these special method names are called. For example:

object.__lt__(*self*, *other*) object.__le__(*self*,
*other*)¶<http://docs.python.org/reference/datamodel.html#object.__le__>
object.__eq__(*self*, *other*) object.__ne__(*self*, *other*) object.__gt__(
*self*, *other*) object.__ge__(*self*, *other*)

New in version 2.1.

These are the so-called “rich comparison” methods, and are called for
comparison operators in preference to
__cmp__()<http://docs.python.org/reference/datamodel.html#object.__cmp__>below.
The correspondence between operator symbols and method names is as
follows: x<y calls x.__lt__(y), x<=y calls x.__le__(y), x==y calls
x.__eq__(y), x!=y and x<>y call x.__ne__(y), x>y calls x.__gt__(y),
and x>=ycalls
x.__ge__(y).
I think there is enough info at this page to answer my question as well as
I need it answered right now. Thanks.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120606/6e9eb2ca/attachment-0001.html>


More information about the Tutor mailing list