Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #84489 > unrolled thread

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

Started byDevin Jeanpierre <jeanpierreda@gmail.com>
First post2015-01-24 12:35 -0800
Last post2015-01-24 15:00 -0800
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Alternative to multi-line lambdas: Assign-anywhere def statements Devin Jeanpierre <jeanpierreda@gmail.com> - 2015-01-24 12:35 -0800
    Re: Alternative to multi-line lambdas: Assign-anywhere def statements Yawar Amin <yawar.amin@gmail.com> - 2015-01-24 15:00 -0800

#84489 — Re: Alternative to multi-line lambdas: Assign-anywhere def statements

FromDevin Jeanpierre <jeanpierreda@gmail.com>
Date2015-01-24 12:35 -0800
SubjectRe: Alternative to multi-line lambdas: Assign-anywhere def statements
Message-ID<mailman.18096.1422131746.18130.python-list@python.org>
On Sat, Jan 24, 2015 at 11:55 AM, Chris Angelico <rosuav@gmail.com> wrote:
> That's still only able to assign to a key of a dictionary, using the
> function name. There's no way to represent fully arbitrary assignment
> in Python - normally, you can assign to a name, an attribute, a
> subscripted item, etc. (Augmented assignment is a different beast
> altogether, and doesn't really make sense with functions.) There's no
> easy way to say "@stash(dispatch_table_a['asdf'])" and have that end
> up assigning to exactly that.

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.

-- Devin

[toc] | [next] | [standalone]


#84512

FromYawar Amin <yawar.amin@gmail.com>
Date2015-01-24 15:00 -0800
Message-ID<d59af9f9-bc23-4683-b678-f3c3380b0c26@googlegroups.com>
In reply to#84489
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web