Newbie question about string(passing by ref)

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Thu May 10 18:38:17 EDT 2007


lazy a écrit :
> Hi,
> 
> 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.

Once again : in Python, "variables" are just name=>ref pairs in a 
namespace. The concepts of "pass by ref" or pass by val" are meaningless 
here. Args passed to a function *are* references to objects - Python 
doesn't copy objects unless explicitelly asked to.

> I initially thought of breaking the strings into list and passing the
> list instead, but I think there should be an efficient way.

A typical case of premature "optimization" !-)

> So is there a way to pass a const reference to a string?

Yes : be lazy, and don't worry about that.



More information about the Python-list mailing list