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

Chris Angelico rosuav at gmail.com
Sat Jan 24 14:55:49 EST 2015


On Sun, Jan 25, 2015 at 5:56 AM, Ethan Furman <ethan at stoneleaf.us> wrote:
> If the non-generic is what you're concerned about:
>
> # not tested
> dispatch_table_a = {}
> dispatch_table_b = {}
> dispatch_table_c = {}
>
> class dispatch:
>   def __init__(self, dispatch_table):
>     self.dispatch = dispatch_table
>   def __call__(self, func):
>     self.dispatch[func.__name__] = func
>     return func
>
> @dispatch(dispatch_table_a)
> def foo(...):
>    pass

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.

ChrisA



More information about the Python-list mailing list