difference in binding between strings and tuples?
Iwan van der Kleyn
null at null.com
Tue May 13 16:12:25 EDT 2003
I tried to explain a Java minded collegue the concept of name binding or
variable assignement, straight from Python in a Nutshell. (which by the
way is, together with the Cookbook a very niece piece of work). But then
it proved that perhaps I really did not fully grasp all aspects of it
myself. Check out:
>>> a = 1
>>> b = 1
>>> a == b
1
>>> a is b
1
>>> a += 1
>>> b += 1
>>> a == b
1
>>> a is b
1
>>> x = 'test'
>>> y = 'test'
>>> x == y
1
>>> x is y
1
>>> x += 's'
>>> x == y
1
>>> x is y
0
>>> q = (0, 1)
>>> r = (0, 1)
>>> q == r
1
>>> q is r
0
Ok? Testing for equality gives no suprises, but testing for identity
does, especially considering the differences between strings and tuples
(both of which are immutable)
I had expected (a is b) == True, even after the addidtion. But how do I
explain the results for the strings and tuples?
Can I presume x and y to refer to the same string object before the
append ("addition" ) but to two seperate objects afterwards? Why isn't
that true for the tuples? In other words: why the difference in binding
between strings and tuples?
Regards,
iwan
More information about the Python-list
mailing list