How can you copy (clone) a string?

Aahz Maruch aahz at panix.com
Fri Oct 6 12:14:01 EDT 2000


In article <ug0maus4o.fsf at cs.uu.nl>,  <piet at cs.uu.nl> wrote:
>>>>>> aahz at panix.com (Aahz Maruch) (AM) schrijft:
>
>AM> In article <u4s2rwts1.fsf at cs.uu.nl>,  <piet at cs.uu.nl> wrote:
>>> 
>>> i = 0
>>> s = 1000 * 'x'
>>> mem = None
>>> while 1:
>>> i = i+1
>>> mem = (mem, s+`i`)
>
>AM> Blech.  If you're going to do a trick like that, just use a list.
>
>Why? What's wrong with a tuple?

Because it's normally considered immutable, and here you're using it in
a mutable way.  I just don't think it's Pythonic.

>I used the tuple because a list would grow and would temporarily occupy
>double the space while growing. 

That's just as true of what you're doing with the tuple.  But because
most of the storage space is being occupied by the strings, it's lost in
the noise.  Note that my example elsewhere in this thread uses 1MB
strings, so it's even less overhead for the list.
-- 
                      --- Aahz (Copyright 2000 by aahz at pobox.com)

Androgynous poly kinky vanilla queer het    <*>     http://www.rahul.net/aahz/
Hugs and backrubs -- I break Rule 6

There's a difference between a person who gets shit zie doesn't
deserve and a person who gets more shit than zie deserves.  --Aahz



More information about the Python-list mailing list