Newbie question about string(passing by ref)

Steven D'Aprano steve at REMOVE.THIS.cybersource.com.au
Fri May 11 23:43:03 EDT 2007


On Thu, 10 May 2007 14:53:46 -0700, Gary Herron wrote:

> Strings *are* passed by reference *always*. There is no copy and no 
> overhead. Efficiency is not based on length. Since the string is 
> immutable, nothing you do inside the function can change the string 
> outside the function.

Python does NOT support pass by reference. Nor does it do pass by value.
Both of those models might describe what other languages do, but they
don't describe what Python does.

Python's passing model is different from both pass by reference and pass
by value, and there are circumstances where Python seems to be acting as
if it were doing one or the other. But it isn't. The model Python uses is
often (but not often enough...) called "pass by object" or "call by
sharing".

http://effbot.org/zone/call-by-object.htm


Some people argue that because the underlying C implementation of C-Python
uses pass-by-reference of pointers, it's okay to say Python does too.
That's an invalid argument. Not all Python implementations are written in
C. PyPy doesn't even have pointers, being written in Python. Even if it
did, what the underlying engine does internally is irrelevant to what the
Python language does. What Python does is pass _objects_, not values or
references. 


-- 
Steven.




More information about the Python-list mailing list