"if x == None" raises "'NoneType' object is not callable"

gbreed at cix.compulink.co.uk gbreed at cix.compulink.co.uk
Mon Sep 16 09:23:05 EDT 2002


Luc Saffre wrote:

> Strange! Nobody told you to call 'None'! Who knows an explanation?

You have an error here:

>       except KeyError,e:
>          AttributeError,str(e)

You presumably intend to raise that AttributeError.  Here's a corrected 
version with some error reporting:

>>> class Row:
...     def __init__(self):
...             self.__dict__["_values"] = {}
...     def __getattr__(self, name):
...             try:
...                     return self._values[name]
...             except KeyError, e:
...                     print "tried to get", name
...                     raise AttributeError, str(e)
...     def __setattr__(self, name, value):
...             self.__dict__["_values"][name] = value
... 
>>> row = Row()
>>> row is None
0
>>> row == None
tried to get __eq__
tried to get __coerce__
tried to get __cmp__
0

Without the "raise", __getattr__ returns None for __eq__, and the 
interpreter then tries to call it.  Note that "is None" doesn't have this 
problem.


                      Graham



More information about the Python-list mailing list