Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed5.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.007 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'wed,': 0.04; 'changes,': 0.05; 'indentation': 0.07; 'python': 0.07; 'braces': 0.09; 'malloc': 0.09; 'throw': 0.09; 'pm,': 0.11; 'syntax': 0.12; 'def': 0.13; 'wrote:': 0.14; 'coded': 0.16; 'delimit': 0.16; 'furman': 0.16; 'go?': 0.16; 'indent': 0.16; 'python!': 0.16; 'resource,': 0.16; 'return.': 0.16; 'to"': 0.16; 'code.': 0.18; 'header:In-Reply-To:1': 0.22; 'picking': 0.23; 'that?': 0.23; "what's": 0.24; 'subject: -- ': 0.25; 'unable': 0.26; 'regardless': 0.26; 'chris': 0.27; 'function': 0.27; 'message-id:@mail.gmail.com': 0.28; 'raise': 0.29; 'error': 0.29; 'probably': 0.30; 'blocks': 0.31; 'pattern': 0.31; 'throwing': 0.31; 'however,': 0.31; 'called': 0.32; 'to:addr:python-list': 0.32; 'source': 0.32; 'idea': 0.32; 'sometimes': 0.33; 'uses': 0.34; 'apologies': 0.35; 'some': 0.37; 'should': 0.37; 'received:209.85': 0.37; 'apr': 0.38; 'received:google.com': 0.38; 'goes': 0.38; 'less': 0.38; 'but': 0.38; 'to:addr:python.org': 0.39; 'where': 0.39; 'received:209': 0.39; 'how': 0.39; 'would': 0.40; "it's": 0.40; 'header:Received:5': 0.40; '13,': 0.60; '2011': 0.62; 'making': 0.62; 'miss': 0.68; 'flow': 0.69; '12:42': 0.84; 'around?': 0.84; 'bright': 0.84; 'flipping': 0.84; 'gen': 0.84; 'here!': 0.84; 'if:': 0.84; 'inferior': 0.84; 'received:209.85.210.174': 0.84; 'received:mail- iy0-f174.google.com': 0.84; 'comedy': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:date :message-id:subject:from:to:content-type:content-transfer-encoding; bh=9tcMbQQ8xMHJAuFHyar+YoQhjrxeDmuNIxbeqjY9B9c=; b=EebYG90sejVT1uoQOLNe6lEncEEKn+o1V8xEKoJVOUORHs+yzoqJrZpK5ZyRLQ90LA jqfXBflaxXmxn+K2UT7qZXyxC5CdlFOrTTphOEh0qFVnbe07Zl27sWuQpa4FjMMtO2kX b0GEHtva8IkWZZjXQ1U9IG1QZpRVpFwUNBCbM= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; b=P5EC9JxBtBh6xP1Cc+DzjBuLL6uwEAB75j7HYQdqznVM/0nK6Z5kxAzt2Bh3/FyfBC GFmbVXXKVujruCerdOSrz5qL7aioBnzk9rYOcBeckSOH9zgrfadQBeds9XAF1mHH21/M Msp4TiWbTQgwJMeCFwhqAYun5w6dUVyQ0CW6Y= MIME-Version: 1.0 In-Reply-To: <4DA50D89.3090706@stoneleaf.us> References: <8abff237-5ccd-4eb6-85c8-cdc9e87520b7@bl1g2000vbb.googlegroups.com> <87k4eze4dl.fsf@mithlond.arda> <4DA4DB0F.8040509@stoneleaf.us> <1302650129.8890.0.camel@localhost.localdomain> <4da4e856$0$29986$c3e8da3$5496439d@news.astraweb.com> <4DA50D89.3090706@stoneleaf.us> Date: Wed, 13 Apr 2011 13:00:53 +1000 Subject: Re: Feature suggestion -- return if true From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable 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: 62 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1302663657 news.xs4all.nl 81484 [::ffff:82.94.164.166]:37127 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:3111 On Wed, Apr 13, 2011 at 12:42 PM, Ethan Furman wrote: > The indentation for return and raise is the next coded line. =A0List comp= s and > gen exps are basically uber-functions, and regardless of how you categori= ze > them when they finish it is easy to see where control goes to next becaus= e > of indentation. =A0With this idea flow can leave the function with no > indentation clue, making it easy to miss (assuming, of course, it's not s= ome > bright syntax highlighted color). So if I have a function that can early-abort, I should indent after that? def foo(param): resource=3Dmalloc(50000) # Shtarker, zis is Python! We don't malloc her= e! if not resource: return 0 resource[param]=3D5 del resource return 1 Now, this pattern of "attempt to acquire resource, return if unable to" is probably better recoded as "acquire resource and have it throw an error if it can't"; but if you're eyeballing for control-flow changes, a called function throwing an error is even less obvious than an if: return. Where should the indentation go? As I understand it, Python uses indents the way C uses braces - to delimit blocks of code. The only reason to indent in foo() above would be if the if has an else: if not resource: return 0 else: resource[param]=3D5 del resource return 1 or, flipping that the other way around: if resource: resource[param]=3D5 del resource return 1 return 0 but both of these are grossly inferior to: def foo(param): resource=3Dgenerate_resource() resource.dosomething(param,5) return 1 However, what's to tell you that generate_resource() will throw a KaosError if Siegfried is around? (Oh, and apologies to all for picking a different comedy source for my references. Sometimes even a python has to get smart...) Chris Angelico