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: 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: <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 Cc: "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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 On Thu, Nov 20, 2014 at 4:09 PM, Steven D'Aprano 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 "", line 1, in > 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