[Tutor] Equality of numbers and Strings

Emile van Sebille emile at fenx.com
Mon Jan 10 18:42:23 CET 2011


On 1/10/2011 9:23 AM bob gailer said...
> On 1/10/2011 11:51 AM, Emile van Sebille wrote:
>>
>> well, not predictably unless you understand the specifics of the
>> implementation you're running under.
>>
>>
>> >>> from string import letters
>> >>> longstring = letters*100
>> >>> otherstring = letters*100
>> >>> id(longstring)
>> 12491608
>> >>> id (otherstring)
>> 12100288
>> >>> shortstring = letters[:]
>> >>> id(letters)
>> 11573952
>> >>> id(shortstring)
>> 11573952
>> >>>
>>
> In my experiment I found that using * to replicate gave different
> results than using the exact literal. That is why in the program I
> posted I used the equivalent of eval("'" + letters*n + "'") which gives
> different results than eval("letters*n")!
>
> [snip]
>


Hence, not predictably.

I also found it particularly interesting that an explicit copy didn't:
   shortstring = letters[:]

Emile




More information about the Tutor mailing list