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


Groups > comp.lang.python > #84490

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

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python@mrabarnett.plus.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'assign': 0.07; 'assignment': 0.07; 'assigning': 0.09; 'item,': 0.09; 'python': 0.11; 'def': 0.12; 'jan': 0.12; 'attribute,': 0.16; 'dictionary,': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'func': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'name):': 0.16; 'normally,': 0.16; 'received:192.168.1.4': 0.16; 'skip:@ 20': 0.16; 'slight': 0.16; 'subject:def': 0.16; 'subscripted': 0.16; 'wrote:': 0.18; 'command': 0.22; 'header:User-Agent:1': 0.23; 'skip:" 30': 0.26; 'pass': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'chris': 0.29; 'am,': 0.29; "doesn't": 0.30; 'skip:@ 10': 0.30; 'asked': 0.31; 'that.': 0.31; '25,': 0.31; 'class': 0.32; 'sense': 0.34; 'skip:_ 10': 0.34; 'skip:d 20': 0.34; 'really': 0.36; 'represent': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'easy': 0.60; "you're": 0.61; 'different': 0.65; '2015': 0.84; 'ethan': 0.84; 'furman': 0.84; 'subject:Alternative': 0.84; 'theme:': 0.84; 'wine': 0.91
X-CM-Score 0.00
X-CNFS-Analysis v=2.1 cv=Ev9crEsA c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=Vhvw94NMJWsA:10 a=IkcTkHD0fZMA:10 a=EBOSESyhAAAA:8 a=7SpvC85Faqw2NEVBsdsA:9 a=QEXdDO2ut3YA:10
X-AUTH mrabarnett@:2500
Date Sat, 24 Jan 2015 20:35:41 +0000
From MRAB <python@mrabarnett.plus.com>
User-Agent Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.4.0
MIME-Version 1.0
To python-list@python.org
Subject Re: Alternative to multi-line lambdas: Assign-anywhere def statements
References <CAPTjJmrAQgtauH74dwCzb_-6T09D0dLVkxiGrAxJMp_OOwgS0g@mail.gmail.com> <54C3EAD1.2010501@stoneleaf.us> <CAPTjJmohdQbK9_ikh8heaMRbpJ270UW6C1AE2oNVgV_4AoYfQg@mail.gmail.com>
In-Reply-To <CAPTjJmohdQbK9_ikh8heaMRbpJ270UW6C1AE2oNVgV_4AoYfQg@mail.gmail.com>
Content-Type text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.18097.1422131752.18130.python-list@python.org> (permalink)
Lines 52
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1422131752 news.xs4all.nl 2837 [2001:888:2000:d::a6]:55149
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:84490

Show key headers only | View raw


On 2015-01-24 19:55, Chris Angelico wrote:
> 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.
>
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!")

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Alternative to multi-line lambdas: Assign-anywhere def statements MRAB <python@mrabarnett.plus.com> - 2015-01-24 20:35 +0000

csiph-web