Re: What makes an iterator an iterator?

2007-04-19 Thread Steven D'Aprano
On Wed, 18 Apr 2007 01:45:10 -0700, Paul McGuire wrote: For the record, this is what I actually wanted: a four-line self-sorting dictionary: class SortedDict(dict): def __iter__(self): for key in sorted(self.keys()): yield key [snip] Very neat. Why not this?

Re: What makes an iterator an iterator?

2007-04-19 Thread Steven D'Aprano
On Wed, 18 Apr 2007 19:45:50 -0700, Alex Martelli wrote: 7stud [EMAIL PROTECTED] wrote: ... Can you explain some of the details of why this code fails: ... def next(self): for word in Norwegian Blue's have beautiful plumage!.split(): yield word Sure,

Re: What makes an iterator an iterator?

2007-04-19 Thread Steve Holden
7stud wrote: Hi, Thanks for the responses. 7stud [EMAIL PROTECTED] wrote: Can you explain some of the details of why this code fails: --- class Parrot(object): def __iter__(self): return self def __init__(self): self.next = self.next().next def

Re: What makes an iterator an iterator?

2007-04-19 Thread Terry Reedy
7stud [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] | On Apr 18, 8:38 pm, Terry Reedy [EMAIL PROTECTED] wrote: | | One very good way to get an iterator from an iterable is for .__iter__ to | be a generator function. | | Ahhh. That eliminates having to deal with next().next

Re: What makes an iterator an iterator?

2007-04-19 Thread 7stud
On Apr 19, 5:37 am, Steve Holden [EMAIL PROTECTED] wrote: It's nothing to do with the name lookup. Alex mentioned that to remind us that the magic double-under names are looked up on the type rather than the instance... P.next() vs. type(P).next() Where is the double-under name? --

Re: What makes an iterator an iterator?

2007-04-19 Thread 7stud
On Apr 19, 3:12 pm, 7stud [EMAIL PROTECTED] wrote: On Apr 19, 5:37 am, Steve Holden [EMAIL PROTECTED] wrote: It's nothing to do with the name lookup. Alex mentioned that to remind us that the magic double-under names are looked up on the type rather than the instance... P.next() vs.

Re: What makes an iterator an iterator?

2007-04-19 Thread Steve Holden
7stud wrote: On Apr 19, 5:37 am, Steve Holden [EMAIL PROTECTED] wrote: It's nothing to do with the name lookup. Alex mentioned that to remind us that the magic double-under names are looked up on the type rather than the instance... P.next() vs. type(P).next() Where is the

Re: What makes an iterator an iterator?

2007-04-19 Thread Alex Martelli
7stud [EMAIL PROTECTED] wrote: ... repeatedly calls[type(_t).next(t)] As far as I can tell, if the call was actually _t.next(), the code I asked about would work. However, the name look up for 'next' when you All of your following lamentations are predicated on this assumption, it

Re: What makes an iterator an iterator?

2007-04-19 Thread Alex Martelli
Steven D'Aprano [EMAIL PROTECTED] wrote: Calling a generator, such as this next method, returns an iterator object; calling it repeatedly returns many such iterator objects, and never raises StopIteration, thus obviously producing an unending loop. Thank you for that answer Alex, even

Re: What makes an iterator an iterator?

2007-04-19 Thread Steven D'Aprano
On Thu, 19 Apr 2007 20:00:31 -0700, Alex Martelli wrote: class sane(object): def __init__(self, sentence='four scores and twenty years ago'): def agenerator(): for word in sentence.split(): yield word self._thegen = agenerator() def __iter__(self): return

Re: What makes an iterator an iterator?

2007-04-18 Thread I V
On Wed, 18 Apr 2007 15:39:22 +1000, Steven D'Aprano wrote: I thought that an iterator was any object that follows the iterator protocol, that is, it has a next() method and an __iter__() method. ... class Parrot(object): ... def __init__(self): self.next = self._next() self.next

Re: What makes an iterator an iterator?

2007-04-18 Thread Ben Finney
Steven D'Aprano [EMAIL PROTECTED] writes: class Parrot(object): def __iter__(self): return self def __init__(self): self.next = self._next() def _next(self): for word in Norwegian Blue's have beautiful plumage!.split(): yield word Clearly

Re: What makes an iterator an iterator?

2007-04-18 Thread Stefan Rank
on 18.04.2007 07:39 Steven D'Aprano said the following: I thought that an iterator was any object that follows the iterator replace object with instance of a class, i.e. the relevant methods are looked up in the __class__ not in the instance (I think). I had the same troubles trying to

Re: What makes an iterator an iterator?

2007-04-18 Thread Steven D'Aprano
On Wed, 18 Apr 2007 16:58:23 +1000, Ben Finney wrote: Steven D'Aprano [EMAIL PROTECTED] writes: class Parrot(object): def __iter__(self): return self def __init__(self): self.next = self._next() def _next(self): for word in Norwegian Blue's have

Re: What makes an iterator an iterator?

2007-04-18 Thread Steven D'Aprano
On Wed, 18 Apr 2007 06:13:39 +, I V wrote: On Wed, 18 Apr 2007 15:39:22 +1000, Steven D'Aprano wrote: I thought that an iterator was any object that follows the iterator protocol, that is, it has a next() method and an __iter__() method. [snip] i.e., just rename your _next function to

Re: What makes an iterator an iterator?

2007-04-18 Thread Paul McGuire
On Apr 18, 3:32 am, Steven D'Aprano [EMAIL PROTECTED] wrote: On Wed, 18 Apr 2007 06:13:39 +, I V wrote: On Wed, 18 Apr 2007 15:39:22 +1000, Steven D'Aprano wrote: I thought that an iterator was any object that follows the iterator protocol, that is, it has a next() method and an

Re: What makes an iterator an iterator?

2007-04-18 Thread Peter Otten
Steven D'Aprano wrote: class SortedDict(dict): def __iter__(self): for key in sorted(self.keys()): yield key Note that using sorted(self) does not work. That's because sorted() invokes __iter__() if present. To prevent the recursion you can explicitly invoke

Re: What makes an iterator an iterator?

2007-04-18 Thread Georg Brandl
Stefan Rank schrieb: on 18.04.2007 07:39 Steven D'Aprano said the following: I thought that an iterator was any object that follows the iterator replace object with instance of a class, i.e. the relevant methods are looked up in the __class__ not in the instance (I think). I had the same

Re: What makes an iterator an iterator?

2007-04-18 Thread Isaac Rodriguez
class Parrot(object): def __iter__(self): return self def __init__(self): Typo right here self.next = self._next() write: self.next = self._next no parenthesis. def _next(self): for word in Norwegian Blue's have beautiful

Re: What makes an iterator an iterator?

2007-04-18 Thread Isaac Rodriguez
Sorry, my previous post was incomplete. I didn't realized that you implemented _next() as a generator funcition. Besides changing __init__() from self.next = self._next() to self.next = self._next you need to implement __iter__() as: return self.next() class Parrot(object):

Re: What makes an iterator an iterator?

2007-04-18 Thread Alex Martelli
Steven D'Aprano [EMAIL PROTECTED] wrote: I thought that an iterator was any object that follows the iterator protocol, that is, it has a next() method and an __iter__() method. The special methods need to be on the type -- having attributes of those names on the instance doesn't help (applies

Re: What makes an iterator an iterator?

2007-04-18 Thread boggom
I find myself perplexed as to this behaviour. You can not iterate over a dead object! -- http://mail.python.org/mailman/listinfo/python-list

Re: What makes an iterator an iterator?

2007-04-18 Thread [EMAIL PROTECTED]
On Apr 18, 10:36 am, [EMAIL PROTECTED] wrote: I find myself perplexed as to this behaviour. You can not iterate over a dead object! It's not dead, it's restin'. All shagged out over a long squak. -- http://mail.python.org/mailman/listinfo/python-list

Re: What makes an iterator an iterator?

2007-04-18 Thread 7stud
On Apr 18, 8:50 am, [EMAIL PROTECTED] (Alex Martelli) wrote: The special methods need to be on the type -- having attributes of those names on the instance doesn't help (applies to all special methods in the normal, aka newstyle, object model; legacy, aka classic, classes, work by slightly

Re: What makes an iterator an iterator?

2007-04-18 Thread Terry Reedy
An iterator is an object with a .__iter__ method that returns self and a .next method that either returns an object or raises StopIteration. One very good way to get an iterator from an iterable is for .__iter__ to be a generator function. When called, it returns an generator with .__iter__

Re: What makes an iterator an iterator?

2007-04-18 Thread Alex Martelli
7stud [EMAIL PROTECTED] wrote: ... Can you explain some of the details of why this code fails: ... def next(self): for word in Norwegian Blue's have beautiful plumage!.split(): yield word Sure, easily: a loop like for x in y: binds an unnamed temporary variable

Re: What makes an iterator an iterator?

2007-04-18 Thread 7stud
Hi, Thanks for the responses. 7stud [EMAIL PROTECTED] wrote: Can you explain some of the details of why this code fails: --- class Parrot(object): def __iter__(self): return self def __init__(self): self.next = self.next().next def next(self): for

What makes an iterator an iterator?

2007-04-17 Thread Steven D'Aprano
I thought that an iterator was any object that follows the iterator protocol, that is, it has a next() method and an __iter__() method. But I'm having problems writing a class that acts as an iterator. I have: class Parrot(object): def __iter__(self): return self def