why cannot assign to function call

Erik Max Francis max at alcyone.com
Wed Jan 7 20:07:29 EST 2009


Terry Reedy wrote:
> Joe Strout wrote:
> 
>> That's not necessarily true.  If you have
>>
>>   a = "par" + "rot"
>>   b = "parrot"
>>
>> then, most likely (though it depends on how clever the compiler 
>> optimizations are), there are two different string objects containing 
>> the data "parrot".
> 
>  >>> a='par'+'rot'
>  >>> b='parrot'
>  >>> a is b
> True

One exactly doesn't really say much.  It's implementation dependent, and 
depends on the length of the string:

 >>> a = 'this is a much longer ' + 'parrot'
 >>> b = 'this is a much longer parrot'
 >>> a is b
False

In practice, tests like these are pretty much never useful.  It's 
completely implementation dependent when and under what circumstances 
fundamental immutable objects are reused, and it's not useful anyway; 
what you care about is whether two objects are equal or not, not whether 
they're the same object through some optimization behind the scenes.

-- 
Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
  San Jose, CA, USA && 37 18 N 121 57 W && AIM, Y!M erikmaxfrancis
   All delays are dangerous in war.
    -- John Dryden, 1631-1700



More information about the Python-list mailing list