Python code written in 1998, how to improve/change it?

Bengt Richter bokr at
Wed Jan 25 05:38:10 EST 2006

On Tue, 24 Jan 2006 14:58:27 -0600, skip at wrote:

>    Wolfgang> So basically if I want to write a long-running program in
>    Wolfgang> Python, it would make sense to code all functions that are
>    Wolfgang> likely to be called more than once as generators...
>If they need to resume their calculations from where they left off after the
>last yield.
Hm, I wonder how (all untested)

    def foo(x,y):
        return x**2+y**2
    for pair in pairs: print foo(*pair)

would compare to

    def bar(arglist):
        while True:
            x,y = arglist
            yield x**2 + y**2
    barargs = []
    ibar = bar(barargs).next
    for barargs[:] in pairs: print ibar()    

Of course, for simple stuff there's always manual inlining ;-)

    for x, y in pairs: print x**2, y**2

Hm, <bf warning> it might be interesting if one could bind arg list items of
a generator from the outside, e.g.,

    def gfoo(x, y):
        while True:
           yield x**2 + y**2

    ifoo = gfoo('dummy','dummy') # or first pair
    for ifoo.x, ifoo.y in pairs: print

Bengt Richter

More information about the Python-list mailing list