Duncan Booth <[EMAIL PROTECTED]> wrote:
> lazy <[EMAIL PROTECTED]> wrote:
>> I want to pass a string by reference. I understand that strings are
>> immutable, but Im not
>> going to change the string in the function, just to aviod the overhead
>> of copying(when pass-by-value) because the
>> strings are long and this function will be called over and over
>> again.
> You'll find it is pretty hard to get Python to copy a string even if you
> wanted it to. About the only way is to break it apart and then construct a
> new string with the same value from it.
This, of course, is an implementation detail. There are other programming
languages which use "interned" objects and, as far as I know, they are
considered implementation details with undefined semantics. (In other
words you are supposed to ignore this detail and not rely on the identity
of any particular objects except those which are defined as such by the
language).
(An example, in Python, of an object with a defined identity would be
"None" --- all references to "None" are intentionally references to
a single object and it is the most notable case where the "is" operator
is preferred).
That said it seems to be trivially easy to "copy" a short string
in the version of Python I'm using here:
Python 2.4.4 (#2, Oct 20 2006, 00:23:25)
[GCC 4.1.2 20061015 (prerelease) (Debian 4.1.1-16.1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> foo = "foo"
>>> bar = "".join(list(foo))
>>> id(foo); id(bar); foo is bar
-1211235616
-1211235296
False
>>>
--
Jim Dennis,
Starshine: Signed, Sealed, Delivered
--
http://mail.python.org/mailman/listinfo/python-list