Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed1.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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'string': 0.09; 'below)': 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; 'def': 0.12; 'jan': 0.12; 'boolean': 0.16; 'fine.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'supplying': 0.16; 'wrote:': 0.18; 'basically': 0.19; 'passing': 0.19; 'header :User-Agent:1': 0.23; 'necessary.': 0.24; 'sort': 0.25; '(see': 0.26; 'this:': 0.26; 'header:X-Complaints-To:1': 0.27; 'header:In- Reply-To:1': 0.27; 'function': 0.29; "i'm": 0.30; 'code': 0.31; 'but': 0.35; 'there': 0.35; 'hi,': 0.36; 'being': 0.38; 'to:addr :python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'skip:t 30': 0.61; 'such': 0.63; 'flexible,': 0.84; 'received:fios.verizon.net': 0.84; 'safer': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Supply condition in function call Date: Wed, 25 Mar 2015 14:50:19 -0400 References: <87bnjhyohp.fsf@uriel.graune.org> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-98-114-97-173.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.5.0 In-Reply-To: <87bnjhyohp.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: 27 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1427309455 news.xs4all.nl 2841 [2001:888:2000:d::a6]:35725 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:87965 On 3/25/2015 1:29 PM, Manuel Graune wrote: > Hi, > > I'm looking for a way to supply a condition to an if-statement inside a > function body when calling the function. I can sort of get what I want > with using eval (see code below) but I would like to achieve this in a > safer way. If there is a solution which is safer while being > less flexible, that would be fine. Also, supplying the condition as a > string is not necessary. What I want to do is basically like this: > > def test1(a, b, condition="True"): > for i,j in zip(a,b): > c=i+j > if eval(condition): > print("Foo") The standard solution to the above is to have a boolean parameter, such as print_wanted=False. > test1([0,1,2,3],[1,2,3,4],"i+j >4") > print("Bar") For this, follow Ian's suggestion of passing a predicate function. -- Terry Jan Reedy