[Tutor] Equality of numbers and Strings

Stefan Behnel stefan_ml at behnel.de
Mon Jan 10 18:44:24 CET 2011


Emile van Sebille, 10.01.2011 18:42:
> 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")!
>
> Hence, not predictably.
>
> I also found it particularly interesting that an explicit copy didn't:
> shortstring = letters[:]

There's no need to copy an immutable object.

Stefan



More information about the Tutor mailing list