Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #25731

Re: Basic question about speed/coding style/memory

Path csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
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; 'means,': 0.07; 'subject:question': 0.08; 'notation': 0.09; 'path.': 0.09; 'processing,': 0.09; 'skip:# 30': 0.09; 'terminates': 0.09; 'def': 0.10; 'itself.': 0.11; 'sat,': 0.15; 'block:': 0.16; 'doing,': 0.16; 'dwarfed': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 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; 'received:209.85.214.174': 0.21; 'option.': 0.22; 'work,': 0.22; 'raise': 0.24; 'second': 0.24; 'header:In-Reply-To:1': 0.25; 'handling': 0.27; 'execution': 0.27; 'message-id:@mail.gmail.com': 0.27; 'options': 0.27; 'rest': 0.28; 'subject:/': 0.28; 'indentation': 0.29; 'style.': 0.29; 'that.': 0.30; 'function': 0.30; 'error': 0.30; 'code': 0.31; 'raising': 0.33; 'to:addr :python-list': 0.33; 'skip:d 20': 0.34; 'received:google.com': 0.34; 'doing': 0.35; 'pm,': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'but': 0.36; 'level.': 0.36; 'keeps': 0.37; 'two': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'received:209.85.214': 0.39; 'header:Received:5': 0.40; '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
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:to :content-type; bh=t2M/TNT0Bg2QiJvR43H0/fqiT4xToM6VzqdUPbvBpPg=; b=EhMT5JMo+mC4hGv3LoM6JdamXhqNaj8+YDWdmCRcCBFniP8xbInOf3D97kHVKHyL4F 8c0CTL15RuqWq2JdU+kYrsIlVuilCzJwWTVh7I/AzV/zkOWNH5S8hfVWanpX693IhVua hob8UAtybP2WymoBBx77HVWCziQVoLnSJzlpcbfJlRLxJX5P+ZbSv22uY4sRdovEXuzO DaidsU3Zq05vh8JqaEnHptg4eFhFzmsoPMz5mjLpkwWAmsY9dNQ4VzdlVEH1xey/RE1M cYdV4PlfrCMwotLRPzMTGqpnWv5SoWpphYnvFS7E5X0DQgldKZglxPaLAkaBX5n4XiPj iFSg==
MIME-Version 1.0
In-Reply-To <500A5B47.1060805@freenet.de>
References <500A5B47.1060805@freenet.de>
Date Sat, 21 Jul 2012 19:04:12 +1000
Subject Re: Basic question about speed/coding style/memory
From Chris Angelico <rosuav@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2370.1342861455.4697.python-list@python.org> (permalink)
Lines 49
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1342861455 news.xs4all.nl 6965 [2001:888:2000:d::a6]:60779
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:25731

Show key headers only | View raw


On Sat, Jul 21, 2012 at 5:33 PM, Jan Riechers <janpeterr@freenet.de> wrote:
> Block
> #----------------------------------
> if statemente_true:
>         doSomething()
> else:
>         doSomethingElseInstead()
>
> #----------------------------------

This means, to me, that the two options are peers - you do this or you do that.

> versus this block:
> #----------------------------------
> if statement_true:
>         doSomething()
>         return
>
> doSomethingElseInstead()
>
> #----------------------------------

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.

Definitely for error conditions, I would use the second option. The
"fail and bail" notation keeps the entire error handling in one place:

def func(x,y,z):
  if x<0:
    y+=5
    return
  if y<0:
    raise PEBKAC("There's an idiot here somewhere")
  # ... do the rest of the work

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.

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.

ChrisA

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Re: Basic question about speed/coding style/memory Chris Angelico <rosuav@gmail.com> - 2012-07-21 19:04 +1000
  Re: Basic question about speed/coding style/memory 88888 Dihedral <dihedral88888@googlemail.com> - 2012-07-23 14:48 -0700
  Re: Basic question about speed/coding style/memory 88888 Dihedral <dihedral88888@googlemail.com> - 2012-07-23 14:48 -0700

csiph-web