difference in binding between strings and tuples?
Terry Reedy
tjreedy at udel.edu
Tue May 13 19:24:32 EDT 2003
"Iwan van der Kleyn" <null at null.com> wrote in message
news:3ec151a7$0$137$e4fe514c at dreader4.news.xs4all.nl...
> >>> a = 1
> >>> b = 1
> >>> a is b
1
You are generalizing too quickly. Consider
>>> a = 1000
>>> b = 1000
>>> a is b
0
> >>> x = 'test'
> >>> y = 'test'
> >>> x is y
> 1
This is a version dependent internal implementation optimization.
> >>> x += 's'
> >>> x == y
> 1
No, 'tests' != 'test'! You left something out.
> >>> q = (0, 1)
> >>> r = (0, 1)
> >>> q is r
> 0
Again, implentation choice.
> 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)
Whether the implementation merges duplicate immutables or not is an
internal optimization matter. The main use for 'is' is 'x is None'
instead of 'x == None' which is dependable because None is explicitly
a singleton -- like the empty set in set theory. The next most common
use is for newbies to confuse themselves with insufficient data ;-)
(You are not the first to only test a=1; b=1 and stop there with
ints.)
As for your other questions, 'immutable += something' is (in the
absence of side effects) an abbreviation for 'immutable = immutable +
something'. The name is rebound to a new object. However list += seq
abbreviates list.extend(seq) to modify in place.
Terry J. Reedy
More information about the Python-list
mailing list