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: 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 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: <4f272f1f$0$29989$c3e8da3$5496439d@news.astraweb.com> To: Python 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 On Jan 30, 2012, at 7:00 PM, Steven D'Aprano wrote: > On Mon, 30 Jan 2012 12:41:00 -0500, Charles Yeomans wrote: >=20 >> To catch more than one exception type in an except block, one writes >>=20 >> except (A, B, C) as e: >>=20 >> I'm wondering why it was decided to match tuples, but not lists: >>=20 >> except [A, B, C] as e: >=20 > Simplicity. >=20 > If you also allow lists, then why not allow arbitrary sequences? What=20= > about iterators, do you allow them? That could be awkward, because=20 > iterators can only be run through once. Dictionaries are also = iterable,=20 > so once you allow arbitrary iterables, you get dicts. The whole thing=20= > becomes a mess. Better to keep it simple and only allow a single=20 > canonical collection type, and in Python, that type is tuple, not = list. >=20 > Tuples are that canonical collection type because they have a number = of=20 > desirable properties: >=20 > - 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. >=20 > - Consequently the Python virtual machine can create them rapidly and > efficiently. >=20 > - 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. >=20 > - Tuples are ordered, for the times where that matters. >=20 > - 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. >=20 > - 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. >=20 > - Frozensets and sets are ruled out for historical reasons: they = didn't > exist until Python 2.3. Besides, which would you rather write? >=20 > ("abc", "def") > frozenset([abc", "def"]) >=20 > - Sets and lists are ruled out because they are mutable, both require > much more memory, and sets have a heavier computational burden. >=20 >=20 >=20 >> 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". >=20 > Then you are labouring under a misunderstanding. You're not catching a=20= > tuple, because tuples are never thrown. You're catching any of the=20 > exceptions that are contained in that tuple. >=20 > Both lists and tuples *are* single things in themselves. Both lists = and=20 > tuples are containers: >=20 > A list is a single thing that contains other things.=20 >=20 > A tuple is a single thing that contains other things. >=20 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. =20 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 =3D=3D 'Monday' else OSError as e or=20 L =3D [] 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.=20 Charles Yeomans