Weird generator id() behaviour (was Re: Python code written in 1998, how to improve/change it?)

Fredrik Lundh fredrik at pythonware.com
Tue Jan 24 17:19:12 EST 2006


Carl Cerecke wrote:

> It turns out that generators are more efficient than the eval function
> excuting bits of compiled code. About 20-25% faster.

why are you using generators to return things from a function, when
you can just return the things ?

    def f_on():
        print "on"
        action = next_action()
        if action == 'lift':
            return f_on
        elif action == 'push':
            return f_off

    def f_off():
        print "off"
        action = next_action()
        if action == 'lift':
            return f_on
        elif action == 'push':
            return f_off

    state = f_on
    while state:
        state = state()

</F>






More information about the Python-list mailing list