Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #84487
| References | <CAPTjJmrAQgtauH74dwCzb_-6T09D0dLVkxiGrAxJMp_OOwgS0g@mail.gmail.com> <54C3EAD1.2010501@stoneleaf.us> |
|---|---|
| Date | 2015-01-25 06:55 +1100 |
| Subject | Re: Alternative to multi-line lambdas: Assign-anywhere def statements |
| From | Chris Angelico <rosuav@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.18094.1422129357.18130.python-list@python.org> (permalink) |
On Sun, Jan 25, 2015 at 5:56 AM, Ethan Furman <ethan@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
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Alternative to multi-line lambdas: Assign-anywhere def statements Chris Angelico <rosuav@gmail.com> - 2015-01-25 06:55 +1100
csiph-web