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


Groups > comp.lang.python > #81055

Re: Is StopIteration a singleton?

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!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.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'argument': 0.05; 'level,': 0.07; 'none,': 0.07; '(its': 0.09; 'exception,': 0.09; 'type,': 0.09; 'valueerror:': 0.09; 'cc:addr:python-list': 0.11; '(just': 0.16; 'class)': 0.16; 'finds': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'itself,': 0.16; 'sense,': 0.16; 'singleton': 0.16; 'str,': 0.16; 'type)': 0.16; 'value"': 0.16; 'exception': 0.16; 'files.': 0.16; 'so.': 0.16; 'wrote:': 0.18; 'all,': 0.19; 'module': 0.19; 'thu,': 0.19; '(the': 0.22; 'cc:addr:python.org': 0.22; 'pointer': 0.24; 'cc:2**0': 0.24; 'class.': 0.26; 'gets': 0.27; 'header:In-Reply-To:1': 0.27; 'fixed': 0.29; 'raise': 0.29; 'is?': 0.30; 'message- id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; '"",': 0.31; "d'aprano": 0.31; 'steven': 0.31; 'file': 0.32; 'skip:m 30': 0.32; '(most': 0.33; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'false': 0.36; 'instances': 0.36; 'subject:?': 0.36; 'nov': 0.38; 'pm,': 0.38; 'recent': 0.39; 'does': 0.39; "couldn't": 0.39; 'even': 0.60; 'skip:u 10': 0.60; 'name': 0.63; '20,': 0.68; 'special': 0.74; 'dict,': 0.84; 'float,': 0.84; 'shadow': 0.84; 'subject:skip:S 10': 0.84; 'to:none': 0.92; 'imagine': 0.93
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=MLKuZXt+/m5QW4t5FBd87Gq+aBBITCd4ffNVop3tuwE=; b=nGqETJ2JfkZk2lp+lCnHn2LYSPpPM6QNljqyjgjvHzN5SLPA/+ENGZOoyxr59bwtIF 3X8YUzrZCfdU4GkklJybkuFomFKrWr6Bh8MZJALER0EMpJTP/FeXbI4kMSXp8yh4jntI yblZqCH4wkWhvSBVMm20u06Veb9dZnEDZ9sP0m8nSbzEVp9ORJl8l4EXvGQuudOTOqla BuTmKgJDLAeB/lqV4En6iHjSV1d38qc1HucLXnHwIVWIpJGSCV/GDPBGZMhFZi/7QDyF EaOHj6CRCh2hi6e0CWO2jsKJCRfmwnyOGn3qxw0PHQYymuaLYYEegoBUhq9XInlQVb7r xz8A==
MIME-Version 1.0
X-Received by 10.42.197.136 with SMTP id ek8mr6151387icb.41.1416460562014; Wed, 19 Nov 2014 21:16:02 -0800 (PST)
In-Reply-To <546d77a5$0$12899$c3e8da3$5496439d@news.astraweb.com>
References <mailman.16002.1416448354.18130.python-list@python.org> <546d77a5$0$12899$c3e8da3$5496439d@news.astraweb.com>
Date Thu, 20 Nov 2014 16:16:01 +1100
Subject Re: Is StopIteration a singleton?
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.16006.1416460570.18130.python-list@python.org> (permalink)
Lines 49
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1416460570 news.xs4all.nl 2875 [2001:888:2000:d::a6]:50534
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:81055

Show key headers only | View raw


On Thu, Nov 20, 2014 at 4:09 PM, Steven D'Aprano <steve@pearwood.info> wrote:
> StopIteration is not a singleton class.
>
> py> e1 = StopIteration()
> py> e2 = StopIteration()
> py> e1 is e2
> False
>
> It it were, you couldn't support "return value" from generators, since
> the return value gets turned into the argument for StopIteration.

Clearly so. Also, the special case is for StopIteration, not for its
type (just as there is for None, and not NoneType).

> Nor is it a singleton itself: StopIteration (the class) is an instance of
> type, and there are *many* instances of type:
>
>
> py> all(isinstance(T, type) for T in [int, float, str, bool,
> ...     Exception, StopIteration, dict, type])
> True

Indeed; however, there is a fixed pointer PyExc_StopIteration at the C
level, so in a sense, there can be only one StopIteration. Even if you
shadow the name with a subclass, C code will still raise and look for
the original.

> I cannot imagine what they mean by calling StopIteration a singleton.

Okay, so I'm not the only one finds this strange!

>> Why is StopIteration marshallable but no other exception is? Or, if they
>> are marshallable, why does this one get special handling?
>
> No idea. But you can marshal StopIteration itself, but not StopIteration
> instances:
>
> py> marshal.dumps(StopIteration)
> 'S'
> py> marshal.dumps(StopIteration())
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> ValueError: unmarshallable object

Under what circumstances would you marshal an exception type? I don't
use the module at all, I only happened upon it (its docs, to be
precise) by grepping across files.

ChrisA

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


Thread

Is StopIteration a singleton? Chris Angelico <rosuav@gmail.com> - 2014-11-20 12:52 +1100
  Re: Is StopIteration a singleton? Steven D'Aprano <steve@pearwood.info> - 2014-11-20 05:09 +0000
    Re: Is StopIteration a singleton? Chris Angelico <rosuav@gmail.com> - 2014-11-20 16:16 +1100
    Re: Is StopIteration a singleton? Terry Reedy <tjreedy@udel.edu> - 2014-11-20 22:40 -0500
    Re: Is StopIteration a singleton? Chris Angelico <rosuav@gmail.com> - 2014-11-21 15:23 +1100
    Re: Is StopIteration a singleton? Terry Reedy <tjreedy@udel.edu> - 2014-11-20 23:54 -0500

csiph-web