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


Groups > comp.lang.python > #64994

Re: Try-except-finally paradox

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder3.xlned.com!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; ';-)': 0.03; 'mrab': 0.05; 'output': 0.05; 'finally:': 0.07; 'try:': 0.09; 'yeah,': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; 'jan': 0.12; '"w")': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'func():': 0.16; 'prevent': 0.16; 'wrote:': 0.18; 'cc:addr:python.org': 0.22; '31,': 0.24; "shouldn't": 0.24; 'cc:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'am,': 0.29; 'statement': 0.30; 'message-id:@mail.gmail.com': 0.30; 'file': 0.32; 'stuff': 0.32; 'open': 0.33; 'fri,': 0.33; "can't": 0.35; 'done.': 0.35; 'but': 0.35; 'received:google.com': 0.35; "you've": 0.63; 'believe': 0.68; "'with'": 0.84; 'mistake': 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=BP47UOO88SLksrLs6XafVSbQIYHBMWDqWckMVPwlzew=; b=Au0L5hww7P6gl15DccXCyWzfFLeX8zETx+FihyKTKI8E82KC5m7o8RaI+m1fPGn8cn +ftR2sjwU7grnUJ9ClxPv35ExXLsle/EuW6tvvZd7VQkXWIfBecHH+qs+2W4EjLS0yqq c3nbMknH3uQRBdpLph1v82lFIfdSGWqBlkxnmian5ojZWmyiKx08F2UzGLtPZURbguQB nrcLJo9A2cDZppFUXVcpM8ue2hcrE+ZY7NQ3Z7E/JObZi9Fn8W9YriEnB4v6C4vLS7gh PwhPynPNwJYlD7+CXOpXegHsPHdnDeaNq7pq8nR3poObek2CE1hwXQ9zuFSZsgI+Aq57 oGDg==
MIME-Version 1.0
X-Received by 10.66.118.71 with SMTP id kk7mr14307017pab.14.1391087967349; Thu, 30 Jan 2014 05:19:27 -0800 (PST)
In-Reply-To <52EA4F98.2050509@mrabarnett.plus.com>
References <9314ac52-a2be-4382-94ef-2c291f32be1a@googlegroups.com> <lcdf0c$bnl$2@ger.gmane.org> <CAPTjJmrOH6_r-qqc1Wcmcud_HV_N-89vZqQpswao59r08u4+Zg@mail.gmail.com> <52EA4F98.2050509@mrabarnett.plus.com>
Date Fri, 31 Jan 2014 00:19:27 +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.6132.1391087975.18130.python-list@python.org> (permalink)
Lines 18
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1391087975 news.xs4all.nl 2968 [2001:888:2000:d::a6]:33771
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:64994

Show key headers only | View raw


On Fri, Jan 31, 2014 at 12:11 AM, MRAB <python@mrabarnett.plus.com> wrote:
> 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?

Yeah, whoops. The open shouldn't be inside try/finally.

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

But my point still stands, I believe :)

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