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


Groups > comp.lang.python > #64992

Re: Try-except-finally paradox

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'output': 0.05; 'c++,': 0.07; 'finally:': 0.07; 'clause': 0.09; 'exception,': 0.09; 'reached.': 0.09; 'throws': 0.09; 'try:': 0.09; 'runs': 0.10; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; 'jan': 0.12; '"w")': 0.16; 'be:': 0.16; 'called,': 0.16; 'clause.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'func():': 0.16; 'hits': 0.16; 'return,': 0.16; 'wrote:': 0.18; 'bit': 0.19; "python's": 0.19; 'thu,': 0.19; 'putting': 0.22; 'saying': 0.22; 'cc:addr:python.org': 0.22; 'mind.': 0.24; 'cc:2**0': 0.24; 'equivalent': 0.26; 'this:': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; "doesn't": 0.30; 'statement': 0.30; 'message-id:@mail.gmail.com': 0.30; 'usually': 0.31; 'end,': 0.31; 'void': 0.31; 'file': 0.32; 'figure': 0.32; 'stuff': 0.32; 'open': 0.33; 'except': 0.35; 'something': 0.35; 'case,': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'c++': 0.36; 'done': 0.36; 'skip:o 20': 0.38; 'pm,': 0.38; 'anything': 0.39; 'expect': 0.39; 'how': 0.40; 'dave': 0.60; 'matter': 0.61; "you're": 0.61; 'first': 0.61; "you'll": 0.62; 'happen': 0.63; '30,': 0.65; 'finally': 0.65; 'finish': 0.65; 'situation': 0.65; 'close': 0.67; 'statement,': 0.68; "'try'": 0.84; "'with'": 0.84; 'returns.': 0.84; 'angel': 0.91; 'to:none': 0.92
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:cc :content-type; bh=f1kCTVTHFxyy9w+76tSrGCjInoreiyZYOrrBVViw+T0=; b=dUqmOIPIYlcx/PEa4GWCqdj7J054HYucQ2gaXG/a4g3SCj1oPjKPI+hT4xxgyzUKjv jjpJU2AVIE81siaKuIAn/UZePYM8ShZDPlJyX0jD78HqKTuEwkFIup+mz/cXBgcYNqX4 3vBccWuwXOd9niaSsEEcwfIPR2NRzutTsHGYO8XAU4jxgzR5CAPaMUn7A+PNq3jUYTE+ wgbbLrNXhYr3iVZ4PenhG2LXTVrSwnG2ZltJx2NiNLZltytezAfcJaHnzQnMskfqd+Ph ZbMIkLpXgcELWrJraRi6Sg5STX1eAZrokICt/jcQsxi3BZnq5AI8PS6RqxXT91pdbA4S LU8Q==
MIME-Version 1.0
X-Received by 10.68.108.194 with SMTP id hm2mr14417321pbb.22.1391086938658; Thu, 30 Jan 2014 05:02:18 -0800 (PST)
In-Reply-To <lcdf0c$bnl$2@ger.gmane.org>
References <9314ac52-a2be-4382-94ef-2c291f32be1a@googlegroups.com> <lcdf0c$bnl$2@ger.gmane.org>
Date Fri, 31 Jan 2014 00:02:18 +1100
Subject Re: Try-except-finally paradox
From Chris Angelico <rosuav@gmail.com>
Cc "python-list@python.org" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.6130.1391086942.18130.python-list@python.org> (permalink)
Lines 45
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1391086942 news.xs4all.nl 2908 [2001:888:2000:d::a6]:55291
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:64992

Show key headers only | View raw


On Thu, Jan 30, 2014 at 11:05 PM, Dave Angel <davea@davea.name> wrote:
> The finally has to happen before any return inside the try or the
>  except.  And once you're in the finally clause you'll finish it
>  before resuming the except clause.  Since it has a return,  that
>  will happen before the other returns. The one in the except block
>  will never get reached.
>
> It's the only reasonable behavior., to my mind.

It's arguable that putting a return inside a finally is unreasonable
behaviour, but that's up to the programmer. A finally clause can be
used to do what might be done in C++ with a destructor: "no matter how
this function/block exits, do this as you unwind the stack". In C++, I
might open a file like this:

void func()
{
    ofstream output("output.txt");
    // do a whole lot of stuff ...
    // at the close brace, output.~output() will be called, which will
close the file
}

In Python, the equivalent would be:

def func():
    try:
        output = open("output.txt", "w")
        # do a whole lot of stuff ...
    finally:
        output.close()

(Actually, the Python equivalent would be to use a 'with' clause for
brevity, but 'with' uses try/finally under the covers, so it comes to
the same thing.) The concept of the finally clause is: "Whether
execution runs off the end, hits a return statement, or throws an
exception, I need you do this before anything else happens". Having a
return statement inside 'finally' as well as in 'try' is a bit of a
corner case, since you're now saying "Before you finish this function
and return something, I need you to return something else", which
doesn't usually make sense. If you think Python's behaviour is
confusing, first figure out what you would expect to happen in this
situation :)

ChrisA

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


Thread

Try-except-finally paradox Jessica Ross <deathweasel@gmail.com> - 2014-01-29 21:56 -0800
  Re: Try-except-finally paradox Ian Kelly <ian.g.kelly@gmail.com> - 2014-01-29 23:23 -0700
  Re: Try-except-finally paradox Andrew Berg <robotsondrugs@gmail.com> - 2014-01-30 00:33 -0600
    Re: Try-except-finally paradox Rotwang <sg552@hotmail.co.uk> - 2014-01-30 18:12 +0000
      Re: Try-except-finally paradox Ethan Furman <ethan@stoneleaf.us> - 2014-01-30 10:30 -0800
  Re: Try-except-finally paradox wxjmfauth@gmail.com - 2014-01-29 22:59 -0800
  Re:Try-except-finally paradox Dave Angel <davea@davea.name> - 2014-01-30 07:05 -0500
  Re: Try-except-finally paradox Chris Angelico <rosuav@gmail.com> - 2014-01-31 00:02 +1100
  Re: Try-except-finally paradox MRAB <python@mrabarnett.plus.com> - 2014-01-30 13:11 +0000
  Re: Try-except-finally paradox Chris Angelico <rosuav@gmail.com> - 2014-01-31 00:19 +1100
  Re: Try-except-finally paradox Terry Reedy <tjreedy@udel.edu> - 2014-01-31 00:26 -0500
  Re: Try-except-finally paradox Göktuğ Kayaalp <self@gkayaalp.com> - 2014-02-01 03:58 +0200

csiph-web