Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed1a.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.019 X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; 'nicely': 0.07; 'variables': 0.07; 'string': 0.09; 'below)': 0.09; 'function:': 0.09; 'manuel': 0.09; 'def': 0.12; 'fine.': 0.16; 'lambda': 0.16; 'supplying': 0.16; 'wrote:': 0.18; 'basically': 0.19; "python's": 0.19; 'header:User-Agent:1': 0.23; 'expanded': 0.24; 'necessary.': 0.24; '(or': 0.24; 'sort': 0.25; '(see': 0.26; 'this:': 0.26; 'pass': 0.26; 'gets': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'am,': 0.29; "i'm": 0.30; 'code': 0.31; 'gary': 0.31; 'but': 0.35; 'there': 0.35; 'done': 0.36; 'hi,': 0.36; 'too': 0.37; 'received:10': 0.37; 'being': 0.38; 'needed': 0.38; 'whatever': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'skip:t 30': 0.61; 'full': 0.61; 'range': 0.61; 'charset:windows-1252': 0.65; 'institute': 0.72; 'received:10.10': 0.74; 'received:204': 0.75; 'dr.': 0.77; 'flexible,': 0.84; 'safer': 0.84 Date: Wed, 25 Mar 2015 12:13:14 -0700 From: Gary Herron User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.5.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Supply condition in function call References: <87bnjhyohp.fsf@uriel.graune.org> In-Reply-To: <87bnjhyohp.fsf@uriel.graune.org> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit 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: 59 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1427310804 news.xs4all.nl 2867 [2001:888:2000:d::a6]:45729 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:87967 On 03/25/2015 10:29 AM, 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") > > test1([0,1,2,3],[1,2,3,4],"i+j >4") > print("Bar") > test1([0,1,2,3],[1,2,3,4],"c >4") > print("Bar") > test1([0,1,2,3],[1,2,3,4],"a[i] >2") > print("Bar") > test1([0,1,2,3],[1,2,3,4]) > > This is nicely done with lambda expressions: To pass in a condition as a function: test1([0,1,2,3],[1,2,3,4], lambda i,j: i+j<4) To check the condition in the function: if condition(i,j): To get the full range of conditions, you will need to include all the variables needed by any condition you can imagine. So the above suggestions may need to be expanded to: ... lambda i,j,a,b: ... or whatever and ... condition(i,j,a,b) ... or whatever If any of your conditions gets too long/complex for a lambda (or you just don't like Python's lambda expressions), then just create a function for your condition: def cond1(i,j,a,b): return i+j>4 and do test1(..., cond1) and if condition(i,j,a,b): -- Dr. Gary Herron Department of Computer Science DigiPen Institute of Technology (425) 895-4418