Possible Duplicate:
Python '==' vs 'is' comparing strings, 'is' fails sometimes, why?
Hi. I have a question about how Python works when it comes how and when references are used.
I have an example here that I understand.
a = "cat"
b = a
a is b
True
This makes sense. But here comes something I don't understand.
a = "cat"
b = "cat"
a is b
True
c = 1.2
d = 1.2
c is d
False
e = "cat"
f = "".join(a)
e is f
False
Why does a is b return True and not c is d? Both types are immutable right? And It worked when using float numbers I can only imagine it to be some kind of optimization, but I am happy for any answer.
I also tried some other things and got this result:
a = "cat"
b = "c"
c = b+"at"
a is c
False # Why not same as setting c = "cat"
d = "cat"+""
a is d
True # Probably same as setting d = "cat"
e = "c"+"at"
a is e
True # Probably same as setting e = "cat"
I guess this is the same problem here, but why does it not give the True when the variable b is used to create "cat"?
I use python 2.5 if that would make any differance
Any tips and ideas useful here are appreciated.