Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!newsfeed.eweka.nl!eweka.nl!feeder3.eweka.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'classes.': 0.05; 'think,': 0.05; 'backwards': 0.07; 'subject:object': 0.07; 'tests,': 0.07; 'python': 0.08; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'rule.': 0.09; 'subclass': 0.09; 'subject:None': 0.09; 'am,': 0.12; 'exception': 0.12; 'def': 0.13; '"""return': 0.16; 'metaclass': 0.16; 'none"': 0.16; 'roy': 0.16; 'subclasses.': 0.16; 'subclassing': 0.16; 'yes;': 0.16; 'mon,': 0.16; 'wrote:': 0.18; '>>>': 0.18; '(most': 0.21; 'subject:not': 0.21; 'dec': 0.22; "doesn't": 0.22; 'header:In-Reply-To:1': 0.22; 'interface': 0.23; 'traceback': 0.24; 'code': 0.25; 'classes': 0.26; 'code.': 0.26; 'tried': 0.27; 'fact': 0.27; 'anyway.': 0.29; 'temporary': 0.29; 'error': 0.29; 'class': 0.29; 'for,': 0.30; 'subject:Test': 0.30; 'typeerror:': 0.30; 'widget': 0.30; 'chris': 0.30; 'least': 0.30; 'quite': 0.32; 'break': 0.32; "can't": 0.32; 'header:User- Agent:1': 0.33; 'actually': 0.33; 'rather': 0.33; 'header:X -Complaints-To:1': 0.33; 'there': 0.33; 'to:addr:python-list': 0.34; 'probably': 0.34; 'calling': 0.34; 'last):': 0.34; 'however,': 0.36; 'file': 0.36; 'received:au': 0.36; 'none': 0.37; 'acceptable': 0.37; 'class.': 0.37; 'opposed': 0.37; 'but': 0.37; 'using': 0.38; 'received:org': 0.38; 'represent': 0.39; "it's": 0.40; 'to:addr:python.org': 0.40; 'might': 0.40; 'type': 0.61; '2011': 0.61; 'course,': 0.62; 'ever': 0.65; '26,': 0.67; 'failure': 0.74; "'nonetype'": 0.84; '12:17': 0.84; 'article,': 0.84; 'deliberate': 0.84; 'failures.': 0.84; 'scenario': 0.93; 'received:110': 0.95 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Lie Ryan Subject: Re: Test None for an object that does not implement == Date: Mon, 26 Dec 2011 04:13:53 +1100 References: <0d80ce25-9e65-4216-b26c-b8ee40f989a3@q9g2000yqe.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: 110-175-240-90.static.tpgi.com.au User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:8.0) Gecko/20111124 Thunderbird/8.0 In-Reply-To: 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: 47 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1324833250 news.xs4all.nl 6958 [2001:888:2000:d::a6]:56145 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:17922 On 12/26/2011 01:13 AM, Roy Smith wrote: > In article, > Chris Angelico wrote: > >> On Mon, Dec 26, 2011 at 12:17 AM, Roy Smith wrote: >>> Just for fun, I tried playing around with subclassing NoneType and >>> writing an __eq__ for my subclass. Turns out, you can't do that: >>> >>> Traceback (most recent call last): >>> File "./none.py", line 5, in >>> class Nihil(NoneType): >>> TypeError: Error when calling the metaclass bases >>> type 'NoneType' is not an acceptable base type >> >> Yes; unfortunately quite a few Python built-in classes can't be >> subclassed. It's an unfortunate fact of implementation, I think, >> rather than a deliberate rule. >> >> But then, what would you ever need to subclass None for, other than >> toys and testing? > > You might be to differentiate between temporary and permanent failures. > Let's say you have a WidgetPool, containing Widgets of various classes. > > class WidgetPool: > def get_widget(class_name): > """Return a Widget of a given class. If there are no such > Widgets available, returns None.""" > [...] > > You might want to return a None subclass to signify, "No such Widgets > are currently available, but they might be if you try again in a little > while", as opposed to "No such Widgets will ever be available". > > If you were designing the interface from scratch, you would probably > represent that with an exception hierarchy. However, if this was an old > interface that you were modifying, this might be a way to return a > richer failure indication for new clients without breaking backwards > compatibility for existing code. > > Of course, the existing code would probably be using "is None" tests, > and break anyway. But at least that's a plausible scenario for None > subclasses. That scenario doesn't actually need subclassing if you duck type.