Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.011 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'see:': 0.07; 'variables': 0.07; 'file)': 0.09; 'manuel': 0.09; 'pina': 0.09; 'rows': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; 'collections': 0.16; 'csv': 0.16; 'from:addr:cs': 0.16; 'from:addr:zip.com.au': 0.16; 'from:name:cameron simpson': 0.16; 'lambda': 0.16; 'message- id:@cskk.homeip.net': 0.16; 'namedtuple': 0.16; 'parameter.': 0.16; 'simpson': 0.16; 'varnames': 0.16; 'vars:': 0.16; 'write.': 0.16; 'prevent': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'work,': 0.20; 'import': 0.22; 'cc:addr:python.org': 0.22; 'header:User- Agent:1': 0.23; 'button,': 0.24; 'passes': 0.24; 'cheers,': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'header:In-Reply- To:1': 0.27; 'function': 0.29; 'fixed': 0.29; "doesn't": 0.30; 'getting': 0.31; 'easier': 0.31; 'comparison': 0.31; 'doc': 0.31; 'produces': 0.31; 'values.': 0.31; 'writes:': 0.31; 'probably': 0.32; 'url:python': 0.33; 'not.': 0.33; 'device': 0.34; 'agree': 0.35; 'something': 0.35; 'beyond': 0.35; 'but': 0.35; 'there': 0.35; 'charset:us-ascii': 0.36; 'url:org': 0.36; 'wrong': 0.37; 'mapping': 0.38; 'url:library': 0.38; 'read': 0.60; 'url:3': 0.61; 'content-disposition:inline': 0.62; "you'll": 0.62; 'name': 0.63; 'benefit': 0.68; 'sound': 0.68; 'price': 0.69; 'construction': 0.72; 'records': 0.73; 'topic,': 0.81; 'amplified': 0.84; 'pain': 0.84; 'plays': 0.84; 'received:192.168.15': 0.84; 'scraping': 0.84; 'song.': 0.84; 'vars': 0.91; 'miracle': 0.93 Date: Sat, 28 Mar 2015 09:55:57 +1100 From: Cameron Simpson To: Manuel Graune Cc: python-list@python.org Subject: Re: Supply condition in function call MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: <87twx6qkdz.fsf@uriel.graune.org> User-Agent: Mutt/1.5.23 (2014-03-12) References: <87twx6qkdz.fsf@uriel.graune.org> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.19 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: 49 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1427496970 news.xs4all.nl 2865 [2001:888:2000:d::a6]:38409 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:88186 On 27Mar2015 21:02, Manuel Graune wrote: >Cameron Simpson writes: > >> This passes the local variables inside test1() to "condition" as a >> single parameter. Now, I grant that vars['i'] is a miracle of >> tediousness. So consider this elaboration: >> >> from collections import namedtuple >> >> condition_test = lambda vars: vars.i + vars.j > 4 >> >> def test1(a, b, condition): >> for i, j in zip(a,b): >> c = i + j >> vars = locals() >> varnames = list(vars.keys()) >> varstupletype = namedtuple("locals", varnames) >> varstuple = varstupletype(*[ vars[k] for k in varnames ]) >> if condition(varstuple): >> print("Foo") >> >> Here, the condition_test function/lambda uses "vars.i" and "vars.j", >> which i think you'll agree is easier to read and write. The price is >> the construction of a "namedtuple" to hold the variable name >> values. See: >> >> https://docs.python.org/3/library/collections.html#collections.namedtuple >> > >This is probably getting off topic, I think it is on topic. >but is there any relevant difference >or benefit to using namedtuple instead of something like types.SimpleNamespace? > >https://docs.python.org/3/library/types.html#additional-utility-classes-and-functions Probably not. SimpleNamespace is much easier to construct; I hadn't thought of it. As the doc remarks, a namedtuple is probably better for fixed records (eg mapping out rows of a CSV file) because it will prevent you using the wrong name. But for a comparison function SimpleNamespace is probably better. Cheers, Cameron Simpson The Horn of Vengeance: When he pushes the horn button, the device produces the sound of fingernails scraping on a blackboard, amplified beyond pain threshold. If that doesn't work, the horn then plays the Pina Colada song.