Use of the "is" statement
zaikenv at gmail.com
Fri Jun 27 16:51:07 CEST 2008
I'm trying to clarify what exactly the behaviour of the is statement is (or
should be). Naturally, this has been nearly impossible to google for, even
using quotations... It is my impression that the is statement should be
equivalent to "==", at least on some level. However, this equivalency seems
to be inconsistent for reasons I can't decipher. Out of the following 3
cases, only 2 return True. What is the difference (and why is there one) in
the third case?
>>> 'string' is 'string' #simple assignment works
>>> s = 'string'
>>> s is 'string'
>>> def make_string(): return 'test' #but function behaviour varies
>>> def arg_to_str(arg): return str(arg)
>>> make_string() is 'test'
>>> arg_to_string('works') is 'works' # this works
>>> arg_to_string(15) is '15' # but this doesnt
>>> arg_to_string(15) is arg_to_string(15) # nor this!
>>> arg_to_string(15) == arg_to_string(15)
This became a problem when I was using file.tell() and again when using a
custom function. If I am using is in the wrong context, what is the right
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-list