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
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
> >> 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. __
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
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
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)
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. __
> 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
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
> 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
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)
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
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.""
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
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
> 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
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__(
17 matches
Mail list logo