Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'argument': 0.05; 'root': 0.05; 'assign': 0.07; 'odd': 0.07; 'remaining': 0.07; '%s"': 0.09; 'closest': 0.09; 'decorator': 0.09; 'prevents': 0.09; 'def': 0.12; '"is': 0.16; '%r"': 0.16; '-tkc': 0.16; 'callable': 0.16; 'cmd': 0.16; 'dict': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'func': 0.16; 'it."""': 0.16; 'name)': 0.16; 'name):': 0.16; 'naming': 0.16; 'received:174.136': 0.16; 'subclass': 0.16; 'subject: \n ': 0.16; 'subject:def': 0.16; 'there?': 0.16; 'world",': 0.16; 'wrote:': 0.18; 'command': 0.22; 'example': 0.22; 'print': 0.22; 'convenient': 0.24; 'helpful': 0.24; 'looks': 0.24; 'question': 0.24; "i've": 0.25; 'tables': 0.26; 'certain': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'chris': 0.29; '(this': 0.29; "i'm": 0.30; 'asked': 0.31; 'usually': 0.31; '"please': 0.31; 'table,': 0.31; 'class': 0.32; 'skip:m 30': 0.32; 'stuff': 0.32; 'says': 0.33; 'table': 0.34; 'skip:_ 10': 0.34; 'skip:d 20': 0.34; 'could': 0.34; 'something': 0.35; 'but': 0.35; 'there': 0.35; 'yield': 0.36; 'method': 0.36; 'charset:us-ascii': 0.36; 'received:10': 0.37; 'convention': 0.38; 'to:addr:python- list': 0.38; 'short': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'first': 0.61; 'name': 0.63; 'grow': 0.77; 'hardly': 0.84; 'subject:Alternative': 0.84; 'who,': 0.84; 'wine': 0.91 X-Sender-Id: wwwh|x-authuser|tim@thechases.com X-Sender-Id: wwwh|x-authuser|tim@thechases.com X-MC-Relay: Neutral X-MailChannels-SenderId: wwwh|x-authuser|tim@thechases.com X-MailChannels-Auth-Id: wwwh X-MC-Loop-Signature: 1422102104214:649402092 X-MC-Ingress-Time: 1422102104214 Date: Sat, 24 Jan 2015 06:23:38 -0600 From: Tim Chase To: python-list@python.org Subject: Re: Alternative to multi-line lambdas: Assign-anywhere def statements In-Reply-To: References: X-Mailer: Claws Mail 3.8.1 (GTK+ 2.24.10; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-AuthUser: tim@thechases.com 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: 84 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1422102115 news.xs4all.nl 2829 [2001:888:2000:d::a6]:41018 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:84460 On 2015-01-24 17:28, Chris Angelico wrote: > but this is hardly generic. There's no convenient way to give an > argument to a decorator that says "please assign this here", short > of using some stupid eval hack... is there? > > (Incidentally, for a non-generic dispatch table, a callable dict > subclass could be pretty clean: > > class DispatchTable(dict): > """Self-populating dictionary - use this as a function decorator > to stuff functions into it.""" > def __call__(self, func): > self[func.__name__] = func > return func > > cmd = DispatchTable() > > @cmd > def foo(*args): > print("You asked to foo.") As best I can tell, your question is "is there a better way to do dispatch tables without duplicating the storage location while remaining sufficiently generic?" The closest I've seen is something like cmd.Cmd does where you have a class and dispatch methods on it with a certain naming convention (this prevents odd access to dunder methods). Akin to your first example [reordered from your original post] > cmd = {} # Command dispatch table > > def cmd["foo"](*args): > print("You asked to foo.") > > def cmd["bar"](*args): > print("There's no wine in the bar.") > > def cmd["asdf"](*args): > print("Asdf! Asdf!") It usually looks something like class Dispatcher(object): def __getitem__(self, name): return getattr(self, "do_" + name) class MyExample(Dispatcher): def do_hello(self, who): print("Hello, %s" % who) def do_goodbye(self, who, feeling): print("Goodbye, %s. I'm %s to see you go" % ( who, feeling)) mydispatch = MyExample() mydispatch["hello"]("world") mydispatch["goodbye"]("cruel world", "sorry") mydispatch["does not exist"]("give me an AttributeError") The root Dispatcher object can grow helpful methods like class Dispatcher(object): # ... def get_help(self, name): return getattr(self, name, "No help for %r" % name) def __iter__(self): for name in dir(self): if name.startswith("do_") and callable(getattr(self, "do_" + name)): yield name # ... def do_hello(self, who): "Say hello to a person" # ... # print out all known methods and their help for method in mydispatch: print("%s: %s" % (method, mydispatch.get_help(method_name))) -tkc