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

MRAB python at mrabarnett.plus.com
Sat Jan 24 21:35:41 CET 2015


On 2015-01-24 19:55, Chris Angelico wrote:
> 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.
>
Here's a slight variation on the theme:


class DispatchTable(dict):
     def __call__(self, name):
         def setter(func):
             self[name] = func
             return func
         return setter

command = DispatchTable()

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

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

@command("asdf")
def _(*args):
     print("Asdf! Asdf!")




More information about the Python-list mailing list