"if x == None" raises "'NoneType' object is not callable"
Luc Saffre
luc.saffre at gmx.net
Mon Sep 16 09:37:21 EDT 2002
Thank you, Mark. Now I will remeber the difference between "is" and "=="!
There is another big bug in my code snippet. If I write::
try:
return self._values[name]
except KeyError,e:
raise AttributeError,str(e)
instead of::
try:
return self._values[name]
except KeyError,e:
AttributeError,str(e)
then the results are also much more as expected :-)
For every undefined attribute I told Python "yes, I have this attribute
defined, and its value is None".
The explanation for this strange message is now clear (at least for my
simplistic thinking):
- Because I used "==" (and not "is"), Python made a deep look into my
object and looked into each attribute, not only the hash()
- One of these attributes was some of those magic functions, perhaps
__cmp__(). And my __getattr__() returned None instead of raising
AttributeError for these attributes which, I admit, if you define them,
should be callable...
Okay... Python is great!
Luc
On 9/16/2002 4:14 PM, Mark McEahern wrote:
>Comparison to None should generally be comparison by identity rather than
>equality:
>
> if x is None:
>
>or:
>
> if x is not None:
>
>not:
>
> if x == None:
>
>nor:
>
> if x != None:
>
>What you might consider is adding a __len__ magic method to your Row class
>like so:
>
> def __len__(self):
> return len(self.values)
>
>Then you can do this:
>
> r = Row()
> if r:
> ...
> else:
> ...
>
>Cheers,
>
>// mark
>
>-
>
>
>
>
More information about the Python-list
mailing list