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


Groups > comp.lang.python > #88182

Re: Supply condition in function call

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 <python-python-list@m.gmane.org>
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; 'resulting': 0.04; 'argument': 0.05; 'explicitly': 0.05; 'args': 0.07; 'none:': 0.07; 'python3': 0.07; 'variables': 0.07; 'arguments': 0.09; 'convention,': 0.09; 'logic': 0.09; 'manuel': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'suggestions.': 0.09; 'variant': 0.09; 'def': 0.12; '3],': 0.16; '[2,': 0.16; 'cleaner': 0.16; 'code?': 0.16; 'collections': 0.16; 'explicitly,': 0.16; 'lambda': 0.16; 'namedtuple': 0.16; 'optionally': 0.16; 'parameter.': 0.16; 'positional': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'simpson': 0.16; 'variants': 0.16; 'varnames': 0.16; 'vars:': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'basically': 0.19; 'passing': 0.19; 'written': 0.21; '>>>': 0.22; 'example': 0.22; 'import': 0.22; 'header:User-Agent:1': 0.23; 'comparing': 0.24; 'passes': 0.24; 'looks': 0.24; '---': 0.24; 'solutions.': 0.26; 'pass': 0.26; 'header:X-Complaints-To:1': 0.27; 'function': 0.29; 'specifically': 0.29; "i'm": 0.30; 'inspect': 0.31; 'writes:': 0.31; 'sense': 0.34; 'common': 0.35; 'but': 0.35; 'achieving': 0.36; 'list': 0.37; 'clear': 0.37; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'most': 0.60; 'hope': 0.61; 'eye': 0.61; 'trading': 0.61; 'simply': 0.61; 'complete': 0.62; 'name': 0.63; 'more': 0.64; 'different': 0.65; 'natural': 0.68; '20,': 0.68; 'reverse': 0.68; 'otten': 0.84; 'vars': 0.91; 'miracle': 0.93
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: Supply condition in function call
Date Fri, 27 Mar 2015 21:38:54 +0100
Organization None
References <87r3sc9stg.fsf@uriel.graune.org> <20150326070600.GA2199@cskk.homeip.net> <mailman.190.1427360650.10327.python-list@python.org> <87y4miql4h.fsf@uriel.graune.org>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p57bd9671.dip0.t-ipconnect.de
User-Agent KNode/4.13.3
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.276.1427488778.10327.python-list@python.org> (permalink)
Lines 86
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1427488778 news.xs4all.nl 2928 [2001:888:2000:d::a6]:45660
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:88182

Show key headers only | View raw


Manuel Graune wrote:

> Peter Otten <__peter__@web.de> writes:
> 
>> Cameron Simpson wrote:
>>
>>>   test1([0,1,2,3], [1,2,3,4], condition_test)
>>> 
>>> 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())
>>
>> instead or pass the list of arguments explicitly, optionally with some
>> inspect fallback:
>>
>> $ cat pass_condition_inspect.py
>> import inspect
>>
>> def test3(a, b, condition, args=None):
>>     if args is None:
>>         args = inspect.getargspec(condition).args
>>
>>     for i, j in zip(a,b):
>>         c = i + j
>>         _locals = locals()
>>         if condition(*[_locals[name] for name in args]):
>>             print("Foo", i, j)
>>
>> def condition(c, i):
>>     return i * i > c
>>
>> test3([1, 2, 3], [2, 3, 4], condition)
>> print("---")
>> # note reverse order of argument names
>> test3([1, 2, 3], [2, 3, 4], condition, ["i", "c"])
>> $ python3 pass_condition_inspect.py
>> Foo 3 4
>> ---
>> Foo 1 2
>> Foo 2 3
>> Foo 3 4
>>
> 
> I'm just comparing the different solutions. For most of them, the
> logic and the costs/benefits are pretty clear to me. Can you
> elaborate on your code? I'm not clear what you are achieving with
> explicitly passing the arguments instead of simply passing some
> variant of the complete locals()-dictionary.

Positional arguments are the most common calling convention, and the 
resulting condition() function

def condition(c, i):
   return i * i > c

looks more natural to my eye than the

def condition(d):
    return d["i"] * d["i"] > d["c"]

or

def condition(s):
    return d.i * d.i > d.c

variants that you have to write when you follow Cameron's suggestions. You
are basically trading cleaner condition() client functions for messier 
testX() calling code.

You might also be able to reuse functions not written specifically for that 
interface. Example using the sum() builtin:

test3([1, 2, 3], [10, 20, 30], sum, ("a", "j"))

Not that sum() makes much sense here, but I hope you get the idea...

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


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