Re: More baby squeaking - iterators in a class

2004-12-31 Thread M.E.Farmer
py> from __future__ import generators Hello again, I was thinking about the __iter__ and what you were saying about generators, so I opened up pyshell and started typing. py> class R3: ...def __init__(self, d): ... self.d=d ... self.i=len(d) ...def __iter__(self): ... d,i

Re: More baby squeaking - iterators in a class

2004-12-31 Thread M.E.Farmer
Terry , Thank you for the explanation . That is much clearer now, I have played a bit with generators but have never tried to create a custom iterator. I am just now getting into the internals part of python objects... this langauage is still amazing to me! The reason I asked the question was becau

Re: More baby squeaking - iterators in a class

2004-12-31 Thread Terry Reedy
"M.E.Farmer" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Terry Reedy wrote: >>will. Try ip=iter(p) followed by ip.next and ip.next() instead. > Does that mean if you dont't call iter(() on your instance or have a > next() method you can't do this: > p=R3('eggs') > for

Re: More baby squeaking - iterators in a class

2004-12-31 Thread Bulba!
On Fri, 31 Dec 2004 16:31:45 GMT, Steven Bethard <[EMAIL PROTECTED]> wrote: >py> p = R3('eggs') >py> i = p.__iter__() >py> i.next() >'s' >or >py> p = R3('eggs') >py> i = iter(p) >py> i.next() >'s' And that is precisely what I needed to know. Thanks, to you, Terry and everyone who took time to l

Re: More baby squeaking - iterators in a class

2004-12-31 Thread M.E.Farmer
Terry Reedy wrote: >This is the wrong test for what I and some others thought you were asking. >The requirement for p to be an *iterable* and useable in code such as 'for >i in p' is that iter(p), not p itself, have a .next method, and iter(p) >will. Try ip=iter(p) followed by ip.next and ip.next()

Re: More baby squeaking - iterators in a class

2004-12-31 Thread Steven Bethard
Bulba! wrote: Thanks to everyone for their responses, but it still doesn't work re returning next() method: class R3: def __init__(self, d): self.d=d self.i=len(d) def __iter__(self): d,i = self.d, self.i while i>0: i-=1 yield d[i]

Re: More baby squeaking - iterators in a class

2004-12-31 Thread Terry Reedy
"Bulba!" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Thanks to everyone for their responses, but it still doesn't work re > returning next() method: > class R3: >def __init__(self, d): >self.d=d >self.i=len(d) >def __iter__(self): >d,i = self

Re: More baby squeaking - iterators in a class

2004-12-31 Thread M.E.Farmer
Reread Russel Blau post he is spot on with his comments: Russel Blau wrote: >I don't get that from the passage quoted, at all, although it is somewhat >opaque. It says that your __iter__() method must *return an object* with a >next() method; your __iter__() method below doesn't return such an obje

Re: More baby squeaking - iterators in a class

2004-12-31 Thread Bulba!
On Thu, 30 Dec 2004 12:06:31 -0800, Scott David Daniels <[EMAIL PROTECTED]> wrote: >Here's one way: # (Make __iter__ an iterator) >Py> class R1(object): > def __init__(self, data): > self.data = data > self.i = len(data) > def __iter__(self): >

Re: More baby squeaking - iterators in a class

2004-12-30 Thread Scott David Daniels
Bulba! wrote: Hello Mr Everyone, From: http://docs.python.org/tut/node11.html#SECTION001190 "Define a __iter__() method which returns an object with a next() method. If the class defines next(), then __iter__() can just return self:" The thing is, I tried to define __iter__() direct

Re: More baby squeaking - iterators in a class

2004-12-30 Thread Terry Reedy
"Bulba!" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > "Define a __iter__() method which returns an object with a next() > method. If the class defines next(), then __iter__() can just return > self:" > > The thing is, I tried to define __iter__() directly without explicit > defin

Re: More baby squeaking - iterators in a class

2004-12-30 Thread Alex Martelli
Bulba! <[EMAIL PROTECTED]> wrote: > So which is it? Does next() method HAS to be defined > explicitly? It has to be defined, whether explicitly or not (e.g. via a generator). Alex -- http://mail.python.org/mailman/listinfo/python-list

Re: More baby squeaking - iterators in a class

2004-12-30 Thread Russell Blau
"Bulba!" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Hello Mr Everyone, > > From: > http://docs.python.org/tut/node11.html#SECTION001190 > > "Define a __iter__() method which returns an object with a next() > method. If the class defines next(), then __iter__() can

More baby squeaking - iterators in a class

2004-12-30 Thread Bulba!
Hello Mr Everyone, From: http://docs.python.org/tut/node11.html#SECTION001190 "Define a __iter__() method which returns an object with a next() method. If the class defines next(), then __iter__() can just return self:" The thing is, I tried to define __iter__() directly without