Path: csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!news2.euro.net!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'exception': 0.03; '21,': 0.07; 'caller': 0.07; 'means,': 0.07; 'subject:question': 0.08; 'notation': 0.09; 'path.': 0.09; 'processing,': 0.09; 'skip:# 30': 0.09; 'terminates': 0.09; 'to:addr:comp.lang.python': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; 'itself.': 0.11; 'passing': 0.15; 'sat,': 0.15; 'bit.': 0.16; 'block:': 0.16; 'callee': 0.16; 'doing,': 0.16; 'dwarfed': 0.16; "function's": 0.16; 'readability.': 0.16; 'subject:coding': 0.16; 'usage,': 0.16; 'wrote:': 0.17; 'jan': 0.18; 'memory': 0.18; 'parameters': 0.20; 'option.': 0.22; 'work,': 0.22; 'cc:2**0': 0.23; '>': 0.23; 'raise': 0.24; 'cc:no real name:2**0': 0.24; 'second': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header :User-Agent:1': 0.26; 'checking': 0.27; 'handling': 0.27; 'execution': 0.27; 'functions.': 0.27; 'options': 0.27; 'rest': 0.28; 'subject:/': 0.28; 'chris': 0.28; 'indentation': 0.29; 'style.': 0.29; 'that.': 0.30; 'function': 0.30; 'error': 0.30; 'code': 0.31; 'raising': 0.33; 'skip:d 20': 0.34; 'received:google.com': 0.34; 'from:addr:googlemail.com': 0.35; 'doing': 0.35; 'pm,': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'but': 0.36; 'level.': 0.36; 'skip:p 20': 0.36; 'keeps': 0.37; 'does': 0.37; 'two': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'little': 0.39; 'think': 0.40; 'your': 0.60; 'most': 0.61; 'matter': 0.61; 'between': 0.63; 'here': 0.65; 'jul': 0.65; 'unnecessary': 0.65; 'conditions,': 0.84; 'idiot': 0.84; 'significance': 0.84; 'subject:Basic': 0.84 Newsgroups: comp.lang.python Date: Mon, 23 Jul 2012 14:48:37 -0700 (PDT) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=123.192.83.145; posting-account=5JdMBQoAAABHnS4mjpqEzxnmWtgiiVNw References: <500A5B47.1060805@freenet.de> User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-IP: 123.192.83.145 MIME-Version: 1.0 Subject: Re: Basic question about speed/coding style/memory From: 88888 Dihedral To: comp.lang.python@googlegroups.com Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org 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: , Message-ID: Lines: 65 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1343080119 news.xs4all.nl 6901 [2001:888:2000:d::a6]:33779 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:25930 Chris Angelico=E6=96=BC 2012=E5=B9=B47=E6=9C=8821=E6=97=A5=E6=98=9F=E6=9C= =9F=E5=85=ADUTC+8=E4=B8=8B=E5=8D=885=E6=99=8204=E5=88=8612=E7=A7=92=E5=AF= =AB=E9=81=93=EF=BC=9A > On Sat, Jul 21, 2012 at 5:33 PM, Jan Riechers <janpeterr@freenet.de>= ; wrote: > > Block > > #---------------------------------- > > if statemente_true: > > doSomething() > > else: > > doSomethingElseInstead() > > > > #---------------------------------- >=20 > This means, to me, that the two options are peers - you do this or you do= that. >=20 > > versus this block: > > #---------------------------------- > > if statement_true: > > doSomething() > > return > > > > doSomethingElseInstead() > > > > #---------------------------------- >=20 > This would be for an early abort. Don't bother doing most of this > function's work, just doSomething. Might be an error condition, or > perhaps an optimized path. >=20 > Definitely for error conditions, I would use the second option. The > "fail and bail" notation keeps the entire error handling in one= place: >=20 > def func(x,y,z): > if x<0: > y+=3D5 > return > if y<0: > raise PEBKAC("There's an idiot here somewhere") > # ... do the rest of the work >=20 This is the caller responsible style when passing parameters to=20 functions. Checking types of parameters both in the caller and the callee=20 does slow down a little bit. > Note the similarity between the control structures. Raising an > exception immediately terminates processing, without polluting the > rest of the function with an unnecessary indentation level. Early > aborting through normal function return can do the same thing. >=20 > But this is purely a matter of style. I don't think there's any > significance in terms of processing time or memory usage, and even if > there is, it would be dwarfed by considerations of readability. Make > your code look like what it's doing, and let the execution take care > of itself. >=20 > ChrisA