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!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.030 X-Spam-Evidence: '*H*': 0.94; '*S*': 0.00; 'string': 0.09; 'below)': 0.09; 'manuel': 0.09; 'def': 0.12; 'fine.': 0.16; 'lambda': 0.16; 'supplying': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'basically': 0.19; 'necessary.': 0.24; 'sort': 0.25; '(see': 0.26; 'this:': 0.26; 'pass': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; '25,': 0.31; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'hi,': 0.36; 'being': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'mar': 0.68; '2015': 0.84; 'confusing': 0.84; 'flexible,': 0.84; 'safer': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=GZ8jVTAxIIrhrjJQF7y938yFFqT3MTFcSTLYw6rkE6g=; b=W2XG1M1mGR/t1cUkRXNU/ze0/tgyZi1u7j0JzJZwf8Vs56326sUpwFINLWgzknyieE nv4CPF6fd1sjMV4Rbr/B8sYSlZx+ApOy3p6XY0b63vNWGYMYVCPEWcMh9DC1ZzbI9sLf 708FDeZNdUl6VRgtGOjQ///go/wPpBr4pPoBU7yD/jUaO0yJ9xAqqmclvhanlrzdmsjf PgYeS9WGT5DQorEsCs/VX6IpLe5WoKlT3h9+yszW8c1gnGwe1H7MOAwo/GOOM7LECPxk sdpz2UOmxfQvo/OigqQCqA9/KBtZw21lvHOJtdVIPxFAoBzoLFF45cR2y36hHPYcgATM SzNA== X-Received: by 10.70.2.98 with SMTP id 2mr18782386pdt.91.1427305487570; Wed, 25 Mar 2015 10:44:47 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <87bnjhyohp.fsf@uriel.graune.org> References: <87bnjhyohp.fsf@uriel.graune.org> From: Ian Kelly Date: Wed, 25 Mar 2015 11:44:07 -0600 Subject: Re: Supply condition in function call To: Python 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: 34 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1427305497 news.xs4all.nl 2837 [2001:888:2000:d::a6]:34285 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:87956 On Wed, Mar 25, 2015 at 11: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") Pass the condition as a function. def test1(a, b, condition=lambda i, j: True): for i,j in zip(a,b): c=i+j if condition(i, j): print("Foo") test1([0,1,2,3],[1,2,3,4], lambda i, j: i+j > 4) # etc. If you find lambdas confusing and prefer named functions, those will work just as well. def i_plus_j_gt_4(i, j): return i + j > 4 test1([0,1,2,3],[1,2,3,4], i_plus_j_gt_4)