ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> python-tutor
python-tutor
Re: [Tutor] PolyRange -- iterator -- PS
by Spir other posts by this author
Nov 5 2009 1:43AM messages near this date
Re: [Tutor] PolyRange -- iterator | Re: [Tutor] PolyRange -- iterator -- PS
Le Wed, 4 Nov 2009 18:17:21 +0100,
Hugo Arts <hugo.yoshi@[...].com>  s'exprima ainsi:

>  Now, the code. If you write __iter__ as a generator, you won't have to
>  write a next method at all. It simplifies The thing a whole lot:
>  
>  def __iter__(self):
>      for range in self.ranges:
>          for item in range:
>              yield item
>  
>  That's it. Alternatively, you could turn the whole thing into a
>  one-liner and just return a generator expression from __iter__:
>  
>  def __iter__(self):
>      return (item for r in self.ranges for item in r)
>  
>  It's not as clear though, and it doesn't save that much space. I like
>  the first one slightly better.

Thank you very much! That's exactly what I expected. Was sure my code was uselessly heavy. A
ctually, when reading the doc about iteration, I had wrongly understood that next() is requi
red, too.

Now, I agree with you on your last comment... except that (.. for .. in ..) is precisely the
 syntax for generator expression (which is much more familiar to me than the use of generato
r funcs). Still, I will use the first idiom for clarity.

Two additional questions (relative to things manually implemented in my original code):
* What about memorization of "emptyness", meaning the last item is already reached, and foll
owing calls will all fail. This is automatic for generators, but...
* Then how do you restart it? With a decoupling of __iter__() and next(), it's possible to h
ave both failure when empty for the same iterator (= call to next()), and 
a new iterator returned by __iter__(), typically for a new "for" statement. Below after a bu
g correction (attributes needing initialisation):
=======================
def testPolyRange():
    .......
    for i in pr1: print i,
    try:
        print pr1.next()
    except StopIteration,e:
        print "StopIteration"
    for i in pr1: print i,
==> 
1 2 5 6 3 4 5 6 7 8 StopIteration
1 2 5 6 3 4 5 6 7 8
=======================

PS: Just checked and works as expected with generator.

Thank you again, Denis.
------
la vita e estrany


_______________________________________________
Tutor maillist  -  Tutor@[...].org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Thread:
Spir
Hugo Arts
Spir
Hugo Arts
Stefan Lesicnik

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved