[Python-3000] A plea for anonymous functions

Paul Moore p.f.moore at gmail.com
Fri Nov 17 10:57:05 CET 2006


On 11/17/06, Talin <talin at acm.org> wrote:
>     # Create a callback timer
>     alarm = Timer()
>     alarm.SetDuration( 100, Timer.Milliseconds )
>
>     upon alarm:
>        print "It's time to get up, silly-head!"
>
>     print "Alarm has been set!"


Why invent a new special case syntax - the ones we have work fine.

# This goes in a library
def upon(event):
    def wrapper(fn):
        event.set_callback(fn)
        return fn
    return wrapper

# Create a callback timer
alarm = Timer()
alarm.SetDuration( 100, Timer.Milliseconds )

@upon(alarm)
def callback():
    print "It's time to get up, silly-head!"

Looks pretty readable to me...

Paul


More information about the Python-3000 mailing list