comparison with None

Alex Martelli aleax at mac.com
Thu Apr 19 23:29:58 EDT 2007


Steven D'Aprano <steve at REMOVE.THIS.cybersource.com.au> wrote:

> But if you wanted to do extra work unnecessarily, a less unnecessary
> amount of extra work would be:
> 
> if type(x) == type(None): ...

Of course, like the original poster's proposal, this CAN be faked out
(while the 'is' test cannot, one more weird reason why it's better):

>>> class metaWeird(type):
...   def __eq__(self, other): return True
... 
>>> class Weird: __metaclass__ = metaWeird
... 
>>> x = Weird()
>>> type(x) == type(None)
True


(warning to all innocent bystanders: don't try this at home, kids!-)


Alex



More information about the Python-list mailing list