IsString

Mike Meyer mwm at mired.org
Fri Dec 16 03:00:24 CET 2005


Chris Mellon <arkanes at gmail.com> writes:
> I think the issue is pretty simple, myself:
> With mutable objects, Python has the semantics normally associated
> with pass-by-reference.

Except when it doesn't. For instance, if that were true, then in this:

>>> def inc(x):
...  x = x + [1]
... 
>>> y = []
>>> x = y
>>> inc(y)
>>> y
[]
>>> x
>>> []

y would be [1] at the end, not []. The value of x is correct.

> With immutable objects, Python has the semantics normally associated
> with pass-by-value.

When you pass a reference to an immutable object, call-by-value and
call-by-reference have the same semantics. Unless you include object
identity as part of the semantics, in which case Python has
call-by-reference semantics and not call-by-value semantics.

> However, there is *no way* in Python to get pass-by-value semantics
> with a mutable object, or pass-by-reference semantics with an
> immutable one (you can build your own scaffolding to mimic it, but the
> language won't change it's semantics for you).

You do call-by-value in Python the same way you do call-by-reference
in C: you create the appropriate object to pass by hand. And the
semantics of Python argument handling with immutable objects are
closer to call-by-reference than they are to call-by-value.

Mutable/immutable is a red herring, caused because the solution to
wanting to change an immutable object in a function is to pass a
mutable object that refers to the immutable object.

> Therefore, it's more correct to say that Python has neither
> pass-by-reference semantics or pass-by-value semantics, but some third
> thing.

Except that both your points are wrong, casting *serious* doubt on
your conclusion.

> And thus the name pass-by-object, where the calling semantics are
> that you always get a reference to an object,

"You always get a reference to an object"? That sure sounds like
call-by-refence to me. And in fact, every object that Python can
generate will have call-by-reference semantics when passed as an
argument.

The thing is, a common use for call-by-reference is to pass a
reference to a variable. Python doesn't have variables, it has
names. You can't generate a reference to a name (which is one of the
reasons we call them names instead of variables). That meas that you
can't use that idiom in python.

> but what (if any) other names are bound to that object, or if they
> can be bound to that object, depends on the object.

I have no idea what you're trying to say here, but in general what
names are bound to an don't depend on the object at all.

      <mike
-- 
Mike Meyer <mwm at mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.



More information about the Python-list mailing list