Alternative to multi-line lambdas: Assign-anywhere def statements

Yawar Amin yawar.amin at gmail.com
Sat Jan 24 18:00:43 EST 2015


Hi,

On Saturday, January 24, 2015 at 3:36:04 PM UTC-5, Devin Jeanpierre
wrote:
> [...]
> Obviously, nobody will be happy until you can do:
> 
> def call(*a, **kw): return lambda f: f(*a, **kw)
> 
> @call()
> def x, y ():
>     yield 1
>     yield 2
> 
> Actually, maybe not even then.

You're probably right, because someone already _has_ come up with that: 

    https://github.com/carymrobbins/python-blocks/blob/f51d10bd2439990cce7e2cb78e6e56ed05e78648/src/blocks.py#L191

Anyway Chris, to answer your question, the python-blocks repo shows the
'functional decorator style' taken to its logical conclusion, with
decorators for all the various functional building blocks like mapping,
folding, currying, etc.

Incidentally, with a properly-defined decorator 'factory', you could get
a pretty generic decorator for your dispatch example:

    cmd = {}
    dispatch = make_dispatch(cmd)

    @dispatch
    def foo(*args): print("You asked to foo.")

    @dispatch
    def bar(*args): print("There's no wine in the bar.")

    @dispatch
    def cmd2(*args): print("Asdf! Asdf!")

    cmd["foo"]()
    # Etc.

Regards,

Yawar



More information about the Python-list mailing list