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


Groups > comp.lang.python > #19637

Re: except clause syntax question

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <charles@declareSub.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.01; 'exception,': 0.07; 'type,': 0.07; 'python': 0.08; 'ambiguity': 0.09; 'brackets': 0.09; 'dict': 0.09; 'exceptions': 0.09; 'immutable': 0.09; 'iterate': 0.09; 'precedence': 0.09; 'tuple': 0.09; 'tuple.': 0.09; 'exception': 0.12; 'things.': 0.12; 'mon,': 0.15; 'awkward,': 0.16; 'block,': 0.16; 'clauses': 0.16; 'container,': 0.16; 'dictionaries': 0.16; 'efficiently.': 0.16; 'heavier': 0.16; 'immutable,': 0.16; 'insertions': 0.16; 'ioerror': 0.16; 'iterable,': 0.16; 'iterators,': 0.16; 'oserror': 0.16; 'semantically': 0.16; 'simplicity.': 0.16; 'subject:syntax': 0.16; 'tuple,': 0.16; 'tuples,': 0.16; 'subject:question': 0.16; 'wrote:': 0.16; 'rapidly': 0.18; 'jan': 0.19; 'memory': 0.20; 'header:In-Reply-To:1': 0.22; 'statement': 0.23; 'expense': 0.23; 'items.': 0.23; 'once.': 0.23; 'smallest': 0.23; 'modify': 0.25; 'pm,': 0.26; 'function': 0.27; "i'm": 0.27; 'lists': 0.28; 'pass': 0.28; 'fixed': 0.29; 'times.': 0.30; 'list"': 0.30; 'tuples': 0.30; 'x-mailer:apple mail (2.1084)': 0.30; "didn't": 0.30; 'typically': 0.32; 'list': 0.32; 'to:addr:python-list': 0.33; 'match': 0.33; 'this.': 0.33; 'wondering': 0.34; 'list.': 0.34; 'rather': 0.34; 'latter': 0.34; 'round': 0.34; 'set.': 0.34; 'sometimes,': 0.34; 'try:': 0.34; 'typical': 0.34; 'things': 0.35; 'lists,': 0.35; 'sets': 0.35; 'post': 0.36; 'desirable': 0.36; 'opposed': 0.36; 'themselves.': 0.36; 'charset:us-ascii': 0.36; 'run': 0.37; 'machine': 0.37; 'but': 0.37; "there's": 0.37; 'using': 0.37; 'could': 0.37; 'back.': 0.38; 'steven': 0.38; 'hold': 0.38; 'hoping': 0.38; 'think': 0.38; 'why': 0.39; 'except': 0.39; 'else': 0.39; 'sense': 0.39; 'subject:: ': 0.39; 'change': 0.40; 'to:addr:python.org': 0.40; 'under': 0.40; 'type': 0.60; 'types': 0.61; 'more': 0.61; 'your': 0.61; 'carry': 0.62; 'header:Message-Id:1': 0.62; 'charles': 0.67; 'memory,': 0.67; 'collection': 0.68; 'today': 0.70; 'order,': 0.73; '30,': 0.74; '-0500,': 0.84; '2.3.': 0.84; '2012,': 0.84; '7:00': 0.84; 'ruled': 0.84; 'sequences?': 0.84; 'canonical': 0.91; 'lists:': 0.91; 'reasons:': 0.91; 'besides,': 0.93
Content-Type text/plain; charset=us-ascii
Mime-Version 1.0 (Apple Message framework v1084)
Subject Re: except clause syntax question
From Charles Yeomans <charles@declareSub.com>
In-Reply-To <4f272f1f$0$29989$c3e8da3$5496439d@news.astraweb.com>
Date Tue, 31 Jan 2012 08:57:31 -0500
Content-Transfer-Encoding quoted-printable
References <mailman.5218.1327946109.27778.python-list@python.org> <4f272f1f$0$29989$c3e8da3$5496439d@news.astraweb.com>
To Python <python-list@python.org>
X-Mailer Apple Mail (2.1084)
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.5243.1328018257.27778.python-list@python.org> (permalink)
Lines 111
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1328018257 news.xs4all.nl 6869 [2001:888:2000:d::a6]:53326
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:19637

Show key headers only | View raw


On Jan 30, 2012, at 7:00 PM, Steven D'Aprano wrote:

> On Mon, 30 Jan 2012 12:41:00 -0500, Charles Yeomans wrote:
> 
>> To catch more than one exception type in an except block, one writes
>> 
>> except (A, B, C) as e:
>> 
>> I'm wondering why it was decided to match tuples, but not lists:
>> 
>> except [A, B, C] as e:
> 
> Simplicity.
> 
> If you also allow lists, then why not allow arbitrary sequences? What 
> about iterators, do you allow them? That could be awkward, because 
> iterators can only be run through once. Dictionaries are also iterable, 
> so once you allow arbitrary iterables, you get dicts. The whole thing 
> becomes a mess. Better to keep it simple and only allow a single 
> canonical collection type, and in Python, that type is tuple, not list.
> 
> Tuples are that canonical collection type because they have a number of 
> desirable properties:
> 
> - Tuples are small and memory efficient, using the smallest amount of
>  memory needed to hold their items. Lists typically carry a block of
>  spare memory, to make insertions fast.
> 
> - Consequently the Python virtual machine can create them rapidly and
>  efficiently.
> 
> - Tuples are immutable, so you don't have to worry about passing one to a
>  function and having the function modify it behind your back.
> 
> - Tuples are ordered, for the times where that matters.
> 
> - Since the typical use-case is to iterate over the items in fixed order,
>  there's no need to pay the extra expense for a dict or set.
> 
> - Tuples are simple to write: in general you only need commas between
>  items. Sometimes, to avoid ambiguity or change the precedence of
>  calculation, you also need round brackets (parentheses for Americans).
>  Except clauses are one of those times.
> 
> - Frozensets and sets are ruled out for historical reasons: they didn't
>  exist until Python 2.3. Besides, which would you rather write?
> 
>      ("abc", "def")
>      frozenset([abc", "def"])
> 
> - Sets and lists are ruled out because they are mutable, both require
>  much more memory, and sets have a heavier computational burden.
> 
> 
> 
>> The latter makes more sense semantically to me -- "catch all exception
>> types in a list" as opposed to "catch this single thing composed of
>> three exception types".
> 
> Then you are labouring under a misunderstanding. You're not catching a 
> tuple, because tuples are never thrown. You're catching any of the 
> exceptions that are contained in that tuple.
> 
> Both lists and tuples *are* single things in themselves. Both lists and 
> tuples are containers:
> 
> A list is a single thing that contains other things. 
> 
> A tuple is a single thing that contains other things.
> 

I don't think of a tuple as a container, and I don't think it a misunderstanding on my part to think this.  But I am aware that it is common to use tuples as immutable lists.  

I don't see that performance was really a consideration, given that one can use any expression in an except statement --

except IOError if today == 'Monday' else OSError as e

or 

L = []
try:
    #code

except tuple(L) as e:
    pass

except Exception, e:
    L.append(e.__class__)

In any case, though I appreciate your attempt at a post hoc justification, I was hoping for a positive explanation. 

Charles Yeomans

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


Thread

except clause syntax question Charles Yeomans <charles@declareSub.com> - 2012-01-30 12:41 -0500
  Re: except clause syntax question Mel Wilson <mwilson@the-wire.com> - 2012-01-30 14:15 -0500
  Re: except clause syntax question Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-01-31 00:00 +0000
    Re: except clause syntax question Devin Jeanpierre <jeanpierreda@gmail.com> - 2012-01-30 19:25 -0500
    Re: except clause syntax question Charles Yeomans <charles@declareSub.com> - 2012-01-31 08:57 -0500
      Re: except clause syntax question Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-01-31 14:51 +0000
        Re: except clause syntax question Charles Yeomans <charles@declareSub.com> - 2012-01-31 11:23 -0500
        Re: except clause syntax question Devin Jeanpierre <jeanpierreda@gmail.com> - 2012-01-31 11:38 -0500
        Re: except clause syntax question Charles Yeomans <charles@declaresub.com> - 2012-01-31 12:29 -0500
        Re: except clause syntax question Ethan Furman <ethan@stoneleaf.us> - 2012-01-31 08:56 -0800
    Re: except clause syntax question Terry Reedy <tjreedy@udel.edu> - 2012-01-31 19:12 -0500
    Re: except clause syntax question Charles Yeomans <charles@declareSub.com> - 2012-01-31 19:27 -0500
  Re: except clause syntax question Mel Wilson <mwilson@the-wire.com> - 2012-01-31 08:24 -0500
    Re: except clause syntax question Charles Yeomans <charles@declareSub.com> - 2012-01-31 13:04 -0500
  Re: except clause syntax question Duncan Booth <duncan.booth@invalid.invalid> - 2012-01-31 22:03 +0000
    Re: except clause syntax question Chris Angelico <rosuav@gmail.com> - 2012-02-01 11:53 +1100
      Re: except clause syntax question Mel Wilson <mwilson@the-wire.com> - 2012-01-31 23:39 -0500
    Re: except clause syntax question Ian Kelly <ian.g.kelly@gmail.com> - 2012-01-31 18:09 -0700
    Re: except clause syntax question Ian Kelly <ian.g.kelly@gmail.com> - 2012-01-31 18:12 -0700
    Re: except clause syntax question Chris Angelico <rosuav@gmail.com> - 2012-02-01 13:46 +1100

csiph-web