Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #88186
| 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 | <cameron@cskk.homeip.net> |
| 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 <cs@zip.com.au> |
| To | Manuel Graune <manuel.graune@koeln.de> |
| 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 <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.281.1427496970.10327.python-list@python.org> (permalink) |
| 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 |
Show key headers only | View raw
On 27Mar2015 21:02, Manuel Graune <manuel.graune@koeln.de> wrote:
>Cameron Simpson <cs@zip.com.au> 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 <cs@zip.com.au>
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.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Supply condition in function call Manuel Graune <manuel.graune@koeln.de> - 2015-03-25 18:29 +0100
Re: Supply condition in function call Joel Goldstick <joel.goldstick@gmail.com> - 2015-03-25 13:41 -0400
Re: Supply condition in function call Manuel Graune <manuel.graune@koeln.de> - 2015-03-25 18:51 +0100
Re: Supply condition in function call Joel Goldstick <joel.goldstick@gmail.com> - 2015-03-25 14:17 -0400
Re: Supply condition in function call Ian Kelly <ian.g.kelly@gmail.com> - 2015-03-25 12:22 -0600
Re: Supply condition in function call Joel Goldstick <joel.goldstick@gmail.com> - 2015-03-25 14:42 -0400
Re: Supply condition in function call Ian Kelly <ian.g.kelly@gmail.com> - 2015-03-25 11:44 -0600
Re: Supply condition in function call Grant Edwards <invalid@invalid.invalid> - 2015-03-25 19:53 +0000
Re: Supply condition in function call Ian Kelly <ian.g.kelly@gmail.com> - 2015-03-25 14:49 -0600
Re: Supply condition in function call Grant Edwards <invalid@invalid.invalid> - 2015-03-26 15:41 +0000
Re: Supply condition in function call Terry Reedy <tjreedy@udel.edu> - 2015-03-25 14:50 -0400
Re: Supply condition in function call Gary Herron <gherron@digipen.edu> - 2015-03-25 12:13 -0700
Re: Supply condition in function call Rustom Mody <rustompmody@gmail.com> - 2015-03-25 21:02 -0700
Re: Supply condition in function call Chris Angelico <rosuav@gmail.com> - 2015-03-26 17:00 +1100
Re: Supply condition in function call Rustom Mody <rustompmody@gmail.com> - 2015-03-26 18:41 -0700
Re: Supply condition in function call Chris Angelico <rosuav@gmail.com> - 2015-03-27 12:56 +1100
Re: Supply condition in function call Rustom Mody <rustompmody@gmail.com> - 2015-03-26 19:21 -0700
Re: Supply condition in function call Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-03-27 15:34 +1100
Re: Supply condition in function call Rustom Mody <rustompmody@gmail.com> - 2015-03-27 06:48 -0700
Re: Supply condition in function call Chris Angelico <rosuav@gmail.com> - 2015-03-28 01:17 +1100
Re: Supply condition in function call Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-03-27 16:00 +0000
Re: Supply condition in function call Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-03-28 18:32 +1100
Re: Supply condition in function call Chris Angelico <rosuav@gmail.com> - 2015-03-28 18:45 +1100
Re: Supply condition in function call Larry Hudson <orgnut@yahoo.com> - 2015-03-27 17:26 -0700
Re: Supply condition in function call Rustom Mody <rustompmody@gmail.com> - 2015-03-27 18:27 -0700
Re: Supply condition in function call Marko Rauhamaa <marko@pacujo.net> - 2015-03-28 09:50 +0200
Re: Supply condition in function call Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-03-28 20:17 +1100
Re: Supply condition in function call Ian Kelly <ian.g.kelly@gmail.com> - 2015-03-28 03:19 -0600
Re: Supply condition in function call Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-03-28 20:57 +1100
Re: Supply condition in function call Cameron Simpson <cs@zip.com.au> - 2015-03-30 09:35 +1100
Re: Supply condition in function call Manuel Graune <manuel.graune@koeln.de> - 2015-03-30 00:46 +0200
Re: Supply condition in function call Ian Kelly <ian.g.kelly@gmail.com> - 2015-03-26 20:25 -0600
Re: Supply condition in function call Rustom Mody <rustompmody@gmail.com> - 2015-03-26 19:46 -0700
Re: Supply condition in function call Manuel Graune <manuel.graune@koeln.de> - 2015-03-26 07:27 +0100
Re: Supply condition in function call Cameron Simpson <cs@zip.com.au> - 2015-03-26 18:06 +1100
Re: Supply condition in function call Manuel Graune <manuel.graune@koeln.de> - 2015-03-27 21:02 +0100
Re: Supply condition in function call Cameron Simpson <cs@zip.com.au> - 2015-03-28 09:55 +1100
Re: Supply condition in function call Peter Otten <__peter__@web.de> - 2015-03-26 10:03 +0100
Re: Supply condition in function call Manuel Graune <manuel.graune@koeln.de> - 2015-03-27 20:46 +0100
Re: Supply condition in function call Peter Otten <__peter__@web.de> - 2015-03-27 21:38 +0100
Re: Supply condition in function call smap <askme.first@thankyouverymuch.invalid> - 2015-03-28 08:58 +0000
csiph-web