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


Groups > comp.lang.python > #17922

Re: Test None for an object that does not implement ==

From Lie Ryan <lie.1296@gmail.com>
Subject Re: Test None for an object that does not implement ==
Date 2011-12-26 04:13 +1100
References (1 earlier) <pan.2011.12.25.09.38.07.62000@nowhere.com> <mailman.4061.1324811442.27778.python-list@python.org> <roy-5C153F.08172825122011@news.panix.com> <mailman.4066.1324820148.27778.python-list@python.org> <roy-74051C.09130925122011@news.panix.com>
Newsgroups comp.lang.python
Message-ID <mailman.4079.1324833250.27778.python-list@python.org> (permalink)

Show all headers | View raw


On 12/26/2011 01:13 AM, Roy Smith wrote:
> In article<mailman.4066.1324820148.27778.python-list@python.org>,
>   Chris Angelico<rosuav@gmail.com>  wrote:
>
>> On Mon, Dec 26, 2011 at 12:17 AM, Roy Smith<roy@panix.com>  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<module>
>>>     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.

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


Thread

Test None for an object that does not implement == GZ <zyzhu2000@gmail.com> - 2011-12-24 23:09 -0800
  Re: Test None for an object that does not implement == Paul Rubin <no.email@nospam.invalid> - 2011-12-24 23:28 -0800
  Re: Test None for an object that does not implement == Nobody <nobody@nowhere.com> - 2011-12-25 09:38 +0000
    Re: Test None for an object that does not implement == Lie Ryan <lie.1296@gmail.com> - 2011-12-25 22:10 +1100
      Re: Test None for an object that does not implement == Roy Smith <roy@panix.com> - 2011-12-25 08:17 -0500
        Re: Test None for an object that does not implement == Chris Angelico <rosuav@gmail.com> - 2011-12-26 00:35 +1100
          Re: Test None for an object that does not implement == Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-25 13:48 +0000
            Re: Test None for an object that does not implement == Chris Angelico <rosuav@gmail.com> - 2011-12-26 01:04 +1100
            Re: Test None for an object that does not implement == Christian Heimes <lists@cheimes.de> - 2011-12-26 01:37 +0100
          Re: Test None for an object that does not implement == Roy Smith <roy@panix.com> - 2011-12-25 09:13 -0500
            Re: Test None for an object that does not implement == Chris Angelico <rosuav@gmail.com> - 2011-12-26 01:23 +1100
            Re: Test None for an object that does not implement == Lie Ryan <lie.1296@gmail.com> - 2011-12-26 04:13 +1100
    Re: Test None for an object that does not implement == Ian Kelly <ian.g.kelly@gmail.com> - 2011-12-25 19:02 -0700
    Re: Test None for an object that does not implement == Ethan Furman <ethan@stoneleaf.us> - 2011-12-25 20:26 -0800
  Re: Test None for an object that does not implement == Larry Hudson <orgnut@yahoo.com> - 2011-12-25 15:45 -0800
    Re: Test None for an object that does not implement == Devin Jeanpierre <jeanpierreda@gmail.com> - 2011-12-25 19:06 -0500
      Re: Test None for an object that does not implement == Roy Smith <roy@panix.com> - 2011-12-25 19:27 -0500
        Re: Test None for an object that does not implement == Devin Jeanpierre <jeanpierreda@gmail.com> - 2011-12-25 19:58 -0500
    Re: Test None for an object that does not implement == Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-26 00:52 +0000
  Re: Test None for an object that does not implement == Ethan Furman <ethan@stoneleaf.us> - 2011-12-25 20:27 -0800
  Re: Test None for an object that does not implement == Devin Jeanpierre <jeanpierreda@gmail.com> - 2011-12-26 00:22 -0500
    Re: Test None for an object that does not implement == Roy Smith <roy@panix.com> - 2011-12-26 08:43 -0500
  Re: Test None for an object that does not implement == Paul Rudin <paul.nospam@rudin.co.uk> - 2011-12-26 14:33 +0000
  Re: Test None for an object that does not implement == Ethan Furman <ethan@stoneleaf.us> - 2011-12-26 09:14 -0800

csiph-web