Python

is is not the same as equal in Python


1st of December 2006

Don't do the silly misstake that I did today. I improved my code to better support unicode by replacing all plain strings with unicode strings. In there I had code that looked like this:

 if type_ is 'textarea':
    do something

This was changed to:

 if type_ is u'textarea':
    do something

And it no longer matched since type_ was a normal ascii string. The correct wat to do these things is like this:

 if type_ == u'textarea':
     do something
 elif type_ is None:
     do something else

Remember:

 >>> "peter" is u"peter"
 False
 >>> "peter" == u"peter"
 True
 >>> None is None
 True
 >>> None == None
 True



Comment

Show all 10 comments
 

Commenting is currently disabled in Mobile version