Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed3a.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.037 X-Spam-Evidence: '*H*': 0.93; '*S*': 0.00; 'else:': 0.03; 'resulting': 0.04; 'string': 0.09; 'below)': 0.09; 'manuel': 0.09; 'cc:addr :python-list': 0.11; 'def': 0.12; 'fine.': 0.16; 'supplying': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'basically': 0.19; 'command': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'choices': 0.24; 'necessary.': 0.24; 'cc:2**0': 0.24; 'sort': 0.25; '(see': 0.26; 'this:': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'url:mailman': 0.30; 'code': 0.31; '25,': 0.31; 'men': 0.31; 'url:python': 0.33; 'could': 0.34; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'false': 0.36; 'url:listinfo': 0.36; 'thanks': 0.36; 'hi,': 0.36; 'url:org': 0.36; 'being': 0.38; 'question,': 0.38; 'pm,': 0.38; 'sure': 0.39; 'called': 0.40; 'url:mail': 0.40; 'dangerous': 0.60; 'skip:t 30': 0.61; 'sum': 0.64; 'world': 0.66; 'side': 0.67; 'mar': 0.68; 'evaluate': 0.72; '2015': 0.84; 'flexible,': 0.84; 'safer': 0.84; 'joel': 0.91; 'to:none': 0.92; 'hundred': 0.95 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=Ntp/sHd+gZX/AWuNdLEDdOutA+HjREuej1iw/Y4daHA=; b=hHvzpvcfOkoYCqK4C1spH7gKr/JYDKAcBVqm9DvXz9HPqAjfQUVx/4YqK8OoORCEE8 dbyycx4fuZvcchfMlt5IS84ZsfVIn3WSfES2xRFfBE44v9ELRRM+439+85Yp+ap6F7rC IGJnCwFOEn3fzMq9+nn6tPxO5ASd55yeMn82YrUlhi0Zv+CFGtreolZfHe9yoNWiozjR BVERADyEvSk0ZfHS3pdXc+jXfWXIjDVZ4VYqSW1DH2YCrFpuzxZ5z4UiIcfV0zN6Ctvp kIxhO1htqviQT2SKvNdOxVXM5Y8XPSxTdbZrcxYn/25KRuHzg3odPigSOFIuaW0hYQUz b+Gg== MIME-Version: 1.0 X-Received: by 10.107.41.10 with SMTP id p10mr15094204iop.58.1427305263005; Wed, 25 Mar 2015 10:41:03 -0700 (PDT) In-Reply-To: <87bnjhyohp.fsf@uriel.graune.org> References: <87bnjhyohp.fsf@uriel.graune.org> Date: Wed, 25 Mar 2015 13:41:02 -0400 Subject: Re: Supply condition in function call From: Joel Goldstick Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 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: 68 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1427305266 news.xs4all.nl 2851 [2001:888:2000:d::a6]:58632 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:87954 On Wed, Mar 25, 2015 at 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") > I'm not sure I understand your question, but condition will evaluate to True or False regardless, so: if condition: print ("Foo") else: print ("Bar") eval can be dangerous as someone could put some unknown command with side effects in condition > 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]) > > Resulting in > > Foo > Foo > Bar > Foo > Foo > Bar > Foo > Bar > Foo > Foo > Foo > Foo > > Thanks for your help > > Regards, > > Manuel > > -- > A hundred men did the rational thing. The sum of those rational choices was > called panic. Neal Stephenson -- System of the world > http://www.graune.org/GnuPG_pubkey.asc > Key fingerprint = 1E44 9CBD DEE4 9E07 5E0A 5828 5476 7E92 2DB4 3C99 > -- > https://mail.python.org/mailman/listinfo/python-list -- Joel Goldstick http://joelgoldstick.com