walterbyrd a écrit : > Thanx for all the replies, I may be slowly getting it. But, can > anybody explain this? > > >>>>a = 'hello' >>>>b = 'hello' >>>>a is b > > True > >>>>a = 'hello there' >>>>b = 'hello there' >>>>a is b > > False > Python - well, CPython (the reference C implementation) at least - tries to optimize memory usage by "interning" (caching/reusing) string objects when possible. You'll find a similar behaviour with integers: >>> a = 5 >>> b = 5 >>> a is b True >>> a = 50000 >>> b = 50000 >>> a is b False >>>
IOW, this is an implementation detail. FWIW, you should not use identity tests on immutable objects (the None object set aside) - since they are immutable, equality test is enough. -- http://mail.python.org/mailman/listinfo/python-list