Re: [Tutor] special attributes naming confusion

2012-06-09 Thread Alan Gauld
On 09/06/12 09:26, Steven D'Aprano wrote: These methods are really only of interest to someone who wants to define their own abstract data type and make it look like a built in I mostly agree with this, more or less, although I would like to correct your terminology. "Abstract data type" does

Re: [Tutor] special attributes naming confusion

2012-06-09 Thread Steven D'Aprano
Alan Gauld wrote: In general someone "just learning python" doesn't need to know about special names, they just use the operations they define. The only exception being __init__() in class definitions. These methods are really only of interest to someone who wants to define their own abstract

Re: [Tutor] special attributes naming confusion

2012-06-07 Thread Prasad, Ramit
> >> That is, for loops first try to build an iterator by calling __iter__, and > if > >> that fails they try the sequence protocol obj[0], obj[1], obj[2], ... > > > > So...I could instead write __getitem__ for the same effect? > > > Er, no... __getitem__ and __iter__ do very different things. __

Re: [Tutor] special attributes naming confusion

2012-06-07 Thread Emile van Sebille
On 6/6/2012 4:28 PM Steven D'Aprano said... Prasad, Ramit wrote: That is, for loops first try to build an iterator by calling __iter__, and if that fails they try the sequence protocol obj[0], obj[1], obj[2], ... So...I could instead write __getitem__ for the same effect? Er, no... __getite

Re: [Tutor] special attributes naming confusion

2012-06-06 Thread Alan Gauld
On 06/06/12 21:32, Dave wrote: I'm not sure where this comment belongs, but I want to share my perspective on the documentation of these special method names. In the following section there is an inconsistency which could be confusing to someone just learning Python (e.g., me). In general someo

Re: [Tutor] special attributes naming confusion

2012-06-06 Thread Steven D'Aprano
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)

Re: [Tutor] special attributes naming confusion

2012-06-06 Thread Steven D'Aprano
Prasad, Ramit wrote: That is, for loops first try to build an iterator by calling __iter__, and if that fails they try the sequence protocol obj[0], obj[1], obj[2], ... So...I could instead write __getitem__ for the same effect? Er, no... __getitem__ and __iter__ do very different things. __

Re: [Tutor] special attributes naming confusion

2012-06-06 Thread Prasad, Ramit
> That is, for loops first try to build an iterator by calling __iter__, and if > that fails they try the sequence protocol obj[0], obj[1], obj[2], ... So...I could instead write __getitem__ for the same effect? Ramit Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology 712 Mai

Re: [Tutor] special attributes naming confusion

2012-06-06 Thread Steven D'Aprano
Prasad, Ramit wrote: For instance, the __iter__() method is called by the iter() built-in. It can also called by any other piece of code that needs to acquire an iterator from a generic container object. There is no list of all such pieces of code that could ever call the __iter__() method of yo

Re: [Tutor] special attributes naming confusion

2012-06-06 Thread Prasad, Ramit
> For instance, the __iter__() method is called by the iter() built-in. > It can also called by any other piece of code that needs to acquire an > iterator from a generic container object. There is no list of all > such pieces of code that could ever call the __iter__() method of your A lot of ti

Re: [Tutor] special attributes naming confusion

2012-06-06 Thread Jerry Hill
On Wed, Jun 6, 2012 at 4:32 PM, Dave wrote: > I'm not sure where this comment belongs, but I want to share my perspective > on the documentation of these special method names. In the following section > there is an inconsistency which could be confusing to someone just learning > Python (e.g., me)

Re: [Tutor] special attributes naming confusion

2012-06-06 Thread Dave
I'm not sure where this comment belongs, but I want to share my perspective on the documentation of these special method names. In the following section there is an inconsistency which could be confusing to someone just learning Python (e.g., me). In the sentence on implementing custom mapping obj

Re: [Tutor] special attributes naming confusion

2012-06-06 Thread Dave
On Wed, Jun 6, 2012 at 3:40 PM, Mark Lawrence 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.""

Re: [Tutor] special attributes naming confusion

2012-06-06 Thread Dave
On Wed, Jun 6, 2012 at 3:30 PM, Prasad, Ramit wrote: > > 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

Re: [Tutor] special attributes naming confusion

2012-06-06 Thread Mark Lawrence
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 s

Re: [Tutor] special attributes naming confusion

2012-06-06 Thread Prasad, Ramit
> 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 > under

[Tutor] special attributes naming confusion

2012-06-06 Thread Dave
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__(