[Tutor] special attributes naming confusion
davestechshop at gmail.com
Wed Jun 6 21:19:11 CEST 2012
I was reading some tutorial material on creating iterators. It shows the
following example implementation of an iterator:
"""Iterator for looping over a sequence backwards."""
def __init__(self, data):
self.data = data
self.index = len(data)
if self.index == 0:
self.index = self.index - 1
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.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Tutor