Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.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; 'else:': 0.03; 'main()': 0.05; 'python': 0.08; "'''": 0.09; '__name__': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'statement.': 0.09; 'am,': 0.12; 'def': 0.15; "'__main__':": 0.16; 'main():': 0.16; 'cc:addr:python-list': 0.16; 'syntax': 0.16; 'wrote:': 0.16; 'cc:no real name:2**0': 0.20; 'suggest': 0.20; 'cc:2**0': 0.22; 'header:In-Reply-To:1': 0.22; 'suggestion': 0.26; 'pass': 0.29; 'skip:( 20': 0.29; 'cc:addr:python.org': 0.30; 'preceding': 0.30; 'subject:format': 0.30; 'class': 0.30; 'error.': 0.32; 'to:addr:python-list': 0.33; 'header:User- Agent:1': 0.34; 'skip:# 10': 0.34; 'header:X-Complaints-To:1': 0.35; 'response': 0.37; 'received:75': 0.37; 'but': 0.37; 'received:org': 0.38; 'subject:: ': 0.39; 'header:Mime-Version:1': 0.39; 'else': 0.39; 'to:addr:python.org': 0.39; 'your': 0.61; 'below': 0.62; 'williams': 0.66; 'sfxlen:4': 0.67; 'pfxlen:big': 0.77; '11:53': 0.84; '11:56': 0.84; 'clearer': 0.84; 'right)': 0.84; 'subject:long': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: "Colin J. Williams" Subject: Re: how to format long if conditions Date: Sat, 27 Aug 2011 17:25:10 -0400 References: <4e58a1cc$0$2474$e4fe514c@news2.news.xs4all.nl> <4e59130d$0$2411$e4fe514c@news2.news.xs4all.nl> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Cc: python-list@python.org X-Gmane-NNTP-Posting-Host: 75-119-254-164.dsl.teksavvy.com User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20110624 Thunderbird/5.0 In-Reply-To: <4e59130d$0$2411$e4fe514c@news2.news.xs4all.nl> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 89 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1314480331 news.xs4all.nl 2547 [2001:888:2000:d::a6]:36455 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:12305 On 27-Aug-11 11:53 AM, Hans Mulder wrote: > On 27/08/11 17:16:51, Colin J. Williams wrote: > >> What about: >> cond= isinstance(left, PyCompare) >> and isinstance(right, PyCompare) >> and left.complist[-1] is right.complist[0] >> py_and= PyCompare(left.complist + right.complist[1:])if cond >> else: py_and = PyBooleanAnd(left, right) >> Colin W. > > That's a syntax error. You need to add parenthesis. > > How about: > > cond = ( > isinstance(left, PyCompare) > and isinstance(right, PyCompare) > and left.complist[-1] is right.complist[0] > } > py_and = ( > PyCompare(left.complist + right.complist[1:]) > if cond > else PyBooleanAnd(left, right) > ) > > -- HansM I like your 11:53 message but suggest indenting the if cond as below to make it clearer that it, with the preceding line, is all one statement. Colin W. #!/usr/bin/env python z= 1 class PyCompare: complist = [True, False] def __init__(self): pass left= PyCompare right= PyCompare def isinstance(a, b): return True def PyBooleanAnd(a, b): return True def PyCompare(a): return False z=2 def try1(): ''' Hans Mulder suggestion 03:50 ''' if ( isinstance(left, PyCompare) and isinstance(right, PyCompare) and left.complist[-1] is right.complist[0] ): py_and = PyCompare(left.complist + right.complist[1:]) else: py_and = PyBooleanAnd(left, right) def try2(): ''' cjw response - corrected 11:56 ''' cond= (isinstance(left, PyCompare) and isinstance(right, PyCompare) and left.complist[-1] is right.complist[0]) py_and= (PyCompare(left.complist + right.complist[1:]) if cond else PyBooleanAnd(left, right)) def try3(): ''' Hans Mulder 11:53 ''' cond = ( isinstance(left, PyCompare) and isinstance(right, PyCompare) and left.complist[-1] is right.complist[0] ) # not } py_and = ( PyCompare(left.complist + right.complist[1:]) if cond else PyBooleanAnd(left, right) ) def main(): try1() try2() try3() if __name__ == '__main__': main() pass