[Tutor] special attributes naming confusion
Mark Lawrence
breamoreboy at yahoo.co.uk
Wed Jun 6 21:40:13 CEST 2012
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.
>
>
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
Try this to start with
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.
--
Cheers.
Mark Lawrence.
More information about the Tutor
mailing list