How can you copy (clone) a string?
Bernhard Herzog
herzog at online.de
Tue Oct 3 13:26:40 EDT 2000
Martin von Loewis <loewis at informatik.hu-berlin.de> writes:
> blablu at gmx.net (Mike 'Cat' Perkonigg) writes:
>
> > There seem to be a good optimization :-)
>
> That's the 'strings of length 1' optimization; Python has an array of
> 256 string. The empty string is also a singleton.
>
> In addition, you have the interned strings which are created in
> certain cases.
But string slices that would result in a copy of the whole string are
also optimized themselves:
>>> s = "abc" + chr(64)
>>> s
'abc@'
>>> t = s[:]
>>> s is t
1
a possible solution for the problem if the string is at least two
characters long:
>>> t = s[:1] + s[1:]
>>> s is t
0
--
Bernhard Herzog | Sketch, a drawing program for Unix
herzog at online.de | http://sketch.sourceforge.net/
More information about the Python-list
mailing list