How can you copy (clone) a string?

Mikael Olofsson mikael at isy.liu.se
Tue Oct 3 11:17:37 EDT 2000


On 03-Oct-00 Peter.Rupp at ual.com wrote:
 >  So, my next thought was to simply create thousands of 1-meg strings on 
 >  the fly using "exec".  I could then determine very accurately when the 
 >  process ran out of memory.  (Of course, I could probably write this 
 >  easily in C, but it would be more hastle than it's worth at this point.

Well... FWIW, on my machine I get the following.

>>> a='a'
>>> b='a'
>>> c='a'
>>> d='a'
>>> id(a),id(b),id(c),id(d)
(830696, 830696, 830696, 830696)

But I also get the following.

>>> a=2*'a'
>>> b=2*'a'
>>> c=2*'a'
>>> d=2*'a'
>>> id(a),id(b),id(c),id(d)
(878200, 878280, 878320, 878360)

It seems that as long as you choose long enough strings (2), they do 
wind up in different memory locations.

Someone with deeper knowledge can probably explain why, and tell you
whether you can depend on this behaviour.

/Mikael

-----------------------------------------------------------------------
E-Mail:  Mikael Olofsson <mikael at isy.liu.se>
WWW:     http://www.dtr.isy.liu.se/dtr/staff/mikael               
Phone:   +46 - (0)13 - 28 1343
Telefax: +46 - (0)13 - 28 1339
Date:    03-Oct-00
Time:    17:08:31

         /"\
         \ /     ASCII Ribbon Campaign
          X      Against HTML Mail
         / \

This message was sent by XF-Mail.
-----------------------------------------------------------------------




More information about the Python-list mailing list