Question about Pass-by-object-reference?

Ben Finney ben+python at benfinney.id.au
Tue Jul 22 21:59:45 EDT 2014


fl <rxjwg98 at gmail.com> writes:

> On Tuesday, July 22, 2014 8:27:15 PM UTC-4, Terry Reedy wrote:
> > When you call a function, Python binds function parameter names to
> > argument objects in the function's local namespace, the same as in
> > name assignments. […]
> > Nothing is being 'passed'.
>
> Thanks, but I don't understand your point yet. Could you give me
> another example in which something is passed?

The point being made is that no values are is “passed” in a function
call. If you have learned that term from elsewhere, it doesn't apply
sensibly to Python.

When you have a function ‘foo’ defined to expect a parameter, and you
specify an object (say, the object you have access to by the reference
‘bar’)::

    foo(bar)

What happens is that *the very same object* you're referring to by the
name ‘bar’ is then referenced by a *different* name inside the function
‘foo’. There is no passing; the same object gets a new local name
assigned to it, for use only within that function's code.

Function parameters aren't passed anywhere, they don't go anywhere, they
don't get cast or copied or anything else to the function. The function
gets to refer to the identical object by a name local in that function;
you can see what that parameter's name is by the definition of the
function.

-- 
 \       “Pray, v. To ask that the laws of the universe be annulled in |
  `\     behalf of a single petitioner confessedly unworthy.” —Ambrose |
_o__)                           Bierce, _The Devil's Dictionary_, 1906 |
Ben Finney




More information about the Python-list mailing list