except clause syntax question
steve+comp.lang.python at pearwood.info
Mon Jan 30 19:00:32 EST 2012
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:
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
- 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
- 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?
- 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.
More information about the Python-list