On 2007-08-09, John K Masters <[EMAIL PROTECTED]> wrote: > 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
CPython is full of cute little optimizations, and one of them is that literal strings less than a certain length are 'interned'. This makes them very fast to compare for equality, and cheaper to store (maybe?). > 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. The 10 q's constructed with string arithmetic were not interned, because they were not literals. -- Neil Cerutti -- http://mail.python.org/mailman/listinfo/python-list