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


Groups > comp.lang.python > #64993

Re: Try-except-finally paradox

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python@mrabarnett.plus.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; ';-)': 0.03; '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; 'python': 0.11; 'def': 0.12; 'jan': 0.12; '"w")': 0.16; 'be:': 0.16; 'called,': 0.16; 'clause.': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'func():': 0.16; 'hits': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'received:192.168.1.4': 0.16; 'return,': 0.16; 'prevent': 0.16; 'wrote:': 0.18; 'bit': 0.19; "python's": 0.19; 'thu,': 0.19; 'putting': 0.22; 'saying': 0.22; 'header:User-Agent:1': 0.23; 'mind.': 0.24; 'equivalent': 0.26; 'this:': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'chris': 0.29; "doesn't": 0.30; 'statement': 0.30; 'usually': 0.31; 'end,': 0.31; 'void': 0.31; 'file': 0.32; 'figure': 0.32; 'stuff': 0.32; 'open': 0.33; "can't": 0.35; 'except': 0.35; 'something': 0.35; 'case,': 0.35; 'done.': 0.35; 'but': 0.35; 'c++': 0.36; 'done': 0.36; 'skip:o 20': 0.38; 'to:addr:python- list': 0.38; 'pm,': 0.38; 'anything': 0.39; 'expect': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'dave': 0.60; 'matter': 0.61; "you're": 0.61; 'first': 0.61; "you'll": 0.62; "you've": 0.63; '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; 'mistake': 0.91
X-CM-Score 0.00
X-CNFS-Analysis v=2.1 cv=CfYxutbl c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=5FYZ9MsUIQAA:10 a=ihvODaAuJD4A:10 a=IkcTkHD0fZMA:10 a=EBOSESyhAAAA:8 a=iyrGYwgDhO8A:10 a=_CJ5mBcIvX8luEXRH0MA:9 a=QEXdDO2ut3YA:10
X-AUTH mrabarnett:2500
Date Thu, 30 Jan 2014 13:11:52 +0000
From MRAB <python@mrabarnett.plus.com>
User-Agent Mozilla/5.0 (Windows NT 6.3; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.2.0
MIME-Version 1.0
To python-list@python.org
Subject Re: Try-except-finally paradox
References <9314ac52-a2be-4382-94ef-2c291f32be1a@googlegroups.com> <lcdf0c$bnl$2@ger.gmane.org> <CAPTjJmrOH6_r-qqc1Wcmcud_HV_N-89vZqQpswao59r08u4+Zg@mail.gmail.com>
In-Reply-To <CAPTjJmrOH6_r-qqc1Wcmcud_HV_N-89vZqQpswao59r08u4+Zg@mail.gmail.com>
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
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.6131.1391087514.18130.python-list@python.org> (permalink)
Lines 50
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1391087514 news.xs4all.nl 2945 [2001:888:2000:d::a6]:58527
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:64993

Show key headers only | View raw


On 2014-01-30 13:02, Chris Angelico wrote:
> 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 :)
>
One of the reasons that the 'with' statement was added was to prevent
the mistake that you've just done. ;-)

What if the file can't be opened?

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