Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'assign': 0.07; 'assignment': 0.07; 'assigning': 0.09; 'item,': 0.09; 'cc:addr :python-list': 0.11; 'python': 0.11; 'def': 0.12; 'jan': 0.12; 'attribute,': 0.16; 'dictionary,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'func': 0.16; 'normally,': 0.16; 'skip:@ 20': 0.16; 'subject:def': 0.16; 'subscripted': 0.16; 'wrote:': 0.18; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'skip:" 30': 0.26; 'pass': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'am,': 0.29; "doesn't": 0.30; 'message- id:@mail.gmail.com': 0.30; 'that.': 0.31; '25,': 0.31; 'class': 0.32; 'sense': 0.34; 'skip:_ 10': 0.34; 'received:google.com': 0.35; 'really': 0.36; 'represent': 0.38; 'easy': 0.60; "you're": 0.61; 'different': 0.65; '2015': 0.84; 'ethan': 0.84; 'furman': 0.84; 'subject:Alternative': 0.84; 'to:none': 0.92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=WxB0JxDUARrJrKj2iFWUwmS2qR6ohdyMd8HTZKk+/qM=; b=sz17r45zLR8kj0h/zY7esIE5LzitQlOZ2VWFLbUpKw+renUu34ZrpqD19T6X/enPVu Pho8c6kD1V5xs/bJos/mQZFleg+buUg+TgX/CnTuQ/1oBDMvjKpUH8qjEAelRA6ZNu+I Y030rs7AHjVTpjmpzd1K7fLv4ZXVLuXq1Yei1ooFBRgn1HSXLhsIOeFzreiWZxYC/t1v Gc9MSgWMAlxBdD80JjUWx5uhtmC2j9yHgpKb+AuRZ+V41nNvHaZxSoToB5ND9pEavCSc iqlaM9xIxTzCRleR3Es/BAUwN8KOpzaUzp03WJx/AB4n4qOfsfGCrMRDCJwNDO4Mxjp9 AbjA== MIME-Version: 1.0 X-Received: by 10.140.109.55 with SMTP id k52mr25284925qgf.99.1422129349076; Sat, 24 Jan 2015 11:55:49 -0800 (PST) In-Reply-To: <54C3EAD1.2010501@stoneleaf.us> References: <54C3EAD1.2010501@stoneleaf.us> Date: Sun, 25 Jan 2015 06:55:49 +1100 Subject: Re: Alternative to multi-line lambdas: Assign-anywhere def statements From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 28 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1422129357 news.xs4all.nl 2938 [2001:888:2000:d::a6]:42969 X-Complaints-To: abuse@xs4all.nl Path: csiph.com!usenet.pasdenom.info!bete-des-vosges.org!feed.ac-versailles.fr!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Xref: csiph.com comp.lang.python:84487 On Sun, Jan 25, 2015 at 5:56 AM, Ethan Furman 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