[Tutor] Python vs. MATLAB

Hugo Arts hugo.yoshi at gmail.com
Mon Dec 6 20:05:40 CET 2010


On Mon, Dec 6, 2010 at 6:09 PM, Joel Schwartz <joel at joelschwartz.com> wrote:
> Chris,
>
> Can you say more about number (7) in your list? What does "pass by value"
> mean and what are the alternatives?
>
> Thanks,
> Joel
>

Generally, pass-by-* refers to how the arguments to functions are treated.
* In call-by-value, the value of the arguments are copied into the
function. There is no way to modify variables outside of the function
since you don't have access to them, only to copies. C uses this,
among many others:

int a = 5
void func(int b) { b = 6; }
func(a);
a == 5; /* evaluates to true, variable outside function scope remains
unchanged */

The value b, inside the function, contains a copy of a. So when it is
modified, the original a remains unchanged.

* in call-by-reference, the function is given implicit *references* to
its arguments. When modifying the variables inside of the function,
the variable outside is also changed. you can simulate it in many
languages by passing an expicit reference rather than an implicit one
(such as C's pointers):

int a = 5
void func(int * b) { *b = 6; }
func(&a);
a == 6; /* evaluates to true. the function was able to modify a
variable outside of its scope */

* python uses something that wikipedia calls "call-by-sharing." It is
not call-by-value, nor is it call-by-reference. It means, in short,
that while the function has access to the callers, *values*, it does
NOT have access to the callers *variables*. To demonstrate:

a = []
def f(b):
    b.append(1)
    b = [2]
f(a)
print a # prints "[1]"

As in pass-by-reference, the function f could modify it's callers
values by appending 1 to the list. However, unlike *real*
pass-by-reference, when trying to *re-assign* the variable into
something entirely different, there was no effect (a did not become
equal to [2]).


Many people call python pass-by-reference, even though this is
technically incorrect. The difference comes from the semantics of
variables and values. In languages such as C, a variable is an area of
memory that contains something. An assignment then, copies the value
on the right into the variable (memory) on the left.

python doesn't have variables, but names. a name is essentially itself
a reference to some *object* that lives somewhere in memory. An
assignment is something completely different in this context, it
merely sets the reference (variable) on the left to *point to* the
object on the right. So, when evaluating function arguments, names
inside the function are set to point to the *objects* supplied as
arguments, (not to names!). Since we don't have access to the caller's
names, python is not a true pass-by-reference language.

for more on why python is neither call-by-value nor call-by-reference:
http://effbot.org/zone/call-by-object.htm

for more on python's variable semantics and how it differs from
languages like C:
http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#other-languages-have-variables

Hugo


More information about the Tutor mailing list