Is vs Equality Operator

Terry Reedy tjreedy at udel.edu
Thu May 1 12:46:34 EDT 2008


"Good Z" <goodz158 at yahoo.com> wrote in message 
news:706397.16040.qm at web35903.mail.mud.yahoo.com...
| Hello,
| I am having problem in using is. Here is what i am doing.
|
| x=''
| if x is None or x is '':
|        return 1

x is not the singleton value None, nor is it the newly created null string 
object.  It is up to the implementation whether each instance of '' reuses 
one cached (or interned) null string object or creates another.  Don't 
depend on such behavior unless you really mean it.  Same goes for comparing 
ints.

|
| The above statement does not return value 1.
|
| If i changed the above check to
| if x == None or x == '':
|        return 1
| Now it works fine.

x still is not None but does equal in value ''

I recommend here

if x is None or x == '': ...

tjr






More information about the Python-list mailing list