Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Test None for an object that does not implement ==

Reply
Thread Tools

Test None for an object that does not implement ==

 
 
GZ
Guest
Posts: n/a
 
      12-25-2011
Hi,

I run into a weird problem. I have a piece of code that looks like the
following:

f(...., a=None, c=None):
assert (a==None)==(c==None)


The problem is that == is not implemented sometimes for values in a
and c, causing an exception NotImplementedError.

I ended up doing assert (not a)==(not c), but I think this code has
other issues, for example, when a=[] and c=['a'], the assertion will
fail, although a is not None.

So how do I reliably test if a value is None or not?

Thanks,
gz
 
Reply With Quote
 
 
 
 
Paul Rubin
Guest
Posts: n/a
 
      12-25-2011
GZ <(E-Mail Removed)> writes:
> assert (a==None)==(c==None)...
> So how do I reliably test if a value is None or not?


Equality is the wrong comparison to start with. Use "a is None".
 
Reply With Quote
 
 
 
 
Nobody
Guest
Posts: n/a
 
      12-25-2011
On Sat, 24 Dec 2011 23:09:50 -0800, GZ wrote:

> I run into a weird problem. I have a piece of code that looks like the
> following:
>
> f(...., a=None, c=None):
> assert (a==None)==(c==None)
>
>
> The problem is that == is not implemented sometimes for values in a
> and c, causing an exception NotImplementedError.


I have no idea how that can happen. If a.__eq__(None) returns
NotImplemented, the interpreter should flip the test and perform the
equivalent of None.__eq__(a), which will return False.

> So how do I reliably test if a value is None or not?


As Paul says, use "is" to check whether a value _is_ None. Checking for
equality is almost certainly the wrong thing to do; nothing should compare
equal to None except for None itself, so "x is None" and "x == None"
shouldn't produce different results unless there's a bug in the comparison
method.

 
Reply With Quote
 
Lie Ryan
Guest
Posts: n/a
 
      12-25-2011
On 12/25/2011 08:38 PM, Nobody wrote:

> nothing should compare equal to None except for None itself, so "x is None"
> and "x == None" shouldn't produce different results unless there's a
> bug in the comparison method.


not necessarily, for example:

import random
class OddClass:
def __eq__(self, other):
return [True, False][random.randint(0, 1)]

x = OddClass()
print x == None
print x == None
print x == None
print x == None
print x == None


Now, whether doing something like that is advisable or not, that's a
different question; however nothing in python states that you couldn't
have something that compare equal to None whether there is a bug or not
in the comparison method.

 
Reply With Quote
 
Roy Smith
Guest
Posts: n/a
 
      12-25-2011
In article <(E-Mail Removed)>,
Lie Ryan <(E-Mail Removed)> wrote:

> Now, whether doing something like that is advisable or not, that's a
> different question; however nothing in python states that you couldn't
> have something that compare equal to None whether there is a bug or not
> in the comparison method.


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
 
Reply With Quote
 
Chris Angelico
Guest
Posts: n/a
 
      12-25-2011
On Mon, Dec 26, 2011 at 12:17 AM, Roy Smith <(E-Mail Removed)> 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?

ChrisA
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      12-25-2011
On Mon, 26 Dec 2011 00:35:46 +1100, Chris Angelico wrote:

[...]
>> 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.



I can't think of any other un-subclassable classes other than NoneType.
Which ones are you thinking of?



--
Steven
 
Reply With Quote
 
Chris Angelico
Guest
Posts: n/a
 
      12-25-2011
On Mon, Dec 26, 2011 at 12:48 AM, Steven D'Aprano
<(E-Mail Removed)> wrote:
> I can't think of any other un-subclassable classes other than NoneType.
> Which ones are you thinking of?


I don't remember, but it was mentioned in a thread a little while ago.
Experimentation shows that 'bool' is one of them, though. This may
shed some light:

>>> class Foo(type(iter)):

pass

Traceback (most recent call last):
File "<pyshell#103>", line 1, in <module>
class Foo(type(iter)):
TypeError: type 'builtin_function_or_method' is not an acceptable base type

I think there are certain types that are actually not implemented as
classes, and hence cannot be subclassed. This is almost certainly an
implementation detail though; my testing was done in Py3.2 (on Windows
fwiw).

ChrisA
 
Reply With Quote
 
Roy Smith
Guest
Posts: n/a
 
      12-25-2011
In article <(E-Mail Removed)>,
Chris Angelico <(E-Mail Removed)> wrote:

> On Mon, Dec 26, 2011 at 12:17 AM, Roy Smith <(E-Mail Removed)> 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.
 
Reply With Quote
 
Chris Angelico
Guest
Posts: n/a
 
      12-25-2011
On Mon, Dec 26, 2011 at 1:13 AM, Roy Smith <(E-Mail Removed)> wrote:
> If you were designing the interface from scratch, you would probably
> represent that with an exception hierarchy


Or possibly with "returns a False value", giving the option of None
for none available, False for none will ever be available. Of course,
you then have to guarantee that your live return values will always
boolify as True.

ChrisA
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
DataGrid - BorderStyle="None" and BorderWidth="None" doesn't work for Firefox David Freeman ASP .Net 8 02-16-2011 11:03 AM
testing for valid reference: obj vs. None!=obs vs. obj is not None alf Python 9 12-09-2006 05:00 AM
Is there a built-in method for transforming (1,None,"Hello!") to 1,None,"Hello!"? Daniel Crespo Python 5 11-13-2005 12:52 PM
test test test test test test test Computer Support 2 07-02-2003 06:02 PM



Advertisments