Puzzled by "is"

John K Masters johnmasters at oxtedonline.net
Thu Aug 9 22:21:22 CEST 2007


On 15:53 Thu 09 Aug     , Steve Holden wrote:
> Dick Moores wrote:
> > At 10:46 AM 8/9/2007, Bill Scherer wrote:
> >> Dick Moores wrote:
> [...]
> >> There is only one empty tuple.
> >> Does that clear it up for you?
> > 
> > But isn't that the same as saying, "That's just the reality of 
> > Python; it is what it is."? I want to know why there is only one 
> > empty tuple, but more than one (1,).
> > 
> Why? Because.
> 
> Seriously, it's just an optimization by the implementers. There is no 
> need for more than one empty tuple, since tuples can never be modified 
> once created.
> 
> But they decided not to create (1, ) in advance. They probably knew that 
> hardly anybody would want to create that tuple ;-) [Seriously: if you 
> started trying to predict which tuples would be used you would go 
> insane, but the empty tuple is the most likely candidate].
> 
> > Also,
> >  >>> [] is []
> > False
> > 
> In that case it would definitely NOT make sense to have them the same 
> list. Python always ensures that the [] constructor creates a new list, 
> since that list may be bound to one or more variables and mutated. You 
> wouldn't want
> 
>    a = []
>    b = []
>    a.append("boo!")
> 
> to change b so it was no longer an empty list. If you wanted a and b to 
> reference the same list you would change the second statement to
> 
>    b = a
> 
> regards
>   Steve

OK fiddling around with this and reading the docs I tried:-
a = 'qqqqqqqqqq' #10 q's
b = 'qqqqqqqqqq' #10 q's
a is b
true
c = 'q' * 10
c
'qqqqqqqqqq' #10 q's
d = 'q' * 10
d
'qqqqqqqqqq' #10 q's
c is d
false

So from what I've read "==" tests for equivalence, "is" tests for identity but
that does not explain the behaviour above.

Regards, John
-- 
War is God's way of teaching Americans geography
Ambrose Bierce (1842 - 1914)



More information about the Python-list mailing list