Finding the instance reference of an object

greg greg at cosc.canterbury.ac.nz
Mon Nov 10 22:54:10 EST 2008


Steven D'Aprano wrote:

> But the name isn't the argument. The argument to a function is an object

The *formal* argument *is* a name, and that's what
the phrase "changes to the arguments within the called
procedure" is talking about.

> Take a function foo that takes one formal parameter x. Pass an actual 
> argument y to it. The argument is the object currently bound to y, not 
> the name y. Nothing inside foo can rebind the name y because foo doesn't 
> see the name y, it sees the object.

More to the point, it sees the *name x* rather than the
name y, and rebinding the name x doesn't change the binding
of name y. Therefore, the name y has been passed by value,
not by reference.

> [1] You can pass a string representing the name to a function, which can 
> then use some combination of setattr, globals(), exec etc to work with 
> the name represented by that string.

This would be the Python equivalent of the strategy used
in C to emulate call-by-reference -- and it's needed for
the same reason, i.e. the language itself only provides
call-by-value. So you pass a value that you can manually
dereference to get the same effect.

-- 
Greg



More information about the Python-list mailing list