[Python-bugs-list] [ python-Feature Requests-778859 ] Make the generators working like Sather iterator

SourceForge.net noreply at sourceforge.net
Mon Aug 11 19:36:38 EDT 2003


Feature Requests item #778859, was opened at 2003-07-28 05:52
Message generated for change (Comment added) made by rhettinger
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=355470&aid=778859&group_id=5470

>Category: Parser/Compiler
Group: None
Status: Open
Resolution: None
>Priority: 2
Submitted By: Lionel Thiry (shasckaw)
Assigned to: Nobody/Anonymous (nobody)
Summary: Make the generators working like Sather iterator

Initial Comment:
Primarly, it would only be a syntaxic sugar. But it can
lead to some unexpected opitmisations.
The feature would allow this:

def upto(first, last):
        x=first
        while x<=last:
                yield x
                x=x+1

x=upto(1, 10)

try:
        while 1:
                print x.next()
except StopIteration:
        print "End of loop"

To be written like in Sather or something near it:

def upto(first, last):
#same definition as above

loop:
        print upto!(1, 10)
finally:
        print "End of loop"

As you can see, it's shorter, smarter, and more easy to
understand. The '!' in upto!(1, 10) is important to
emphasize its rôle as a generator. The creators of
Sather tell that this technique can lead to a very high
optimisation of loops.
I hope that this request will be deeply studied because
it is really worth of it.

----------------------------------------------------------------------

>Comment By: Raymond Hettinger (rhettinger)
Date: 2003-08-11 20:36

Message:
Logged In: YES 
user_id=80475

For a change of this magnitude, you would need to write up a 
PEP that summarizes the idea, lists advantages, presents use 
cases, and discusses implementation issues.  The first step is 
to introduce the idea on comp.lang.python to tease out the 
issues.

The satheric approach does not fit well with existing python 
generators or with pythonic style.  

There would need to be a new LOOP statement that functions 
as:
   
  try:
      while 1:
          <code block>
  except StopIteration:
      pass

There would also need to be a new keyword for turning 
regular iterators into satheric iterators, so that "sather(xrange
(20))" would have the effect of 
  1) On the first pass, calling:  it = iter(xrange(20))
  2) On each pass, calling and returning it.next()
  3) Passing through the StopIteration on the last pass

Besides the implementation demands, there are conflicts 
with python style programming.  Controlling loops from the 
inside runs contrary to most current approaches to python 
programming -- a notable exception is the use of callback 
functions in GUI programming.

Other considerations are:
* do we want to have one more way of implementing iterators
* does it fit with pythons goals for being easily readable and 
friendly to first time programmers -- I think the answer is no 
because I find your satheric loop example difficult to follow 
(and the same is doubly true for the sather tutorial's bubble 
sort example).

Without some darned compelling use cases, I believe your 
feature request is doomed.

----------------------------------------------------------------------

Comment By: Lionel Thiry (shasckaw)
Date: 2003-07-28 08:23

Message:
Logged In: YES 
user_id=824691

My example was too simple, sorry.
Here is a doc page about satheric iterators:
http://www.gnu.org/software/sather/Doc/tutorial.html/iterators873.html
And the next one is also pretty interesting.

With the satheric way to write it, one would be able to do this:
loop:
        a=range!(1,10)
        if test(a):
                b=range!(1,10)
        print "("+ a + "," + "b" + ")"

My error is repaired now! :)
The first iterator to stop stop the loop.

----------------------------------------------------------------------

Comment By: Barry A. Warsaw (bwarsaw)
Date: 2003-07-28 07:22

Message:
Logged In: YES 
user_id=12800

The Pythonic way to write the latter (after the def) is:

for x in upto(1, 10):
  print x
else:
  print 'end of loop'


----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=355470&aid=778859&group_id=5470



More information about the Python-bugs-list mailing list