By value or by reference?
jcarlson at uci.edu
Mon Oct 18 21:04:28 CEST 2004
[snip section about Java]
Indeed, I meant in reference to C, but I should have been more precise.
> > Python does name binding with references. That is, each reference is
> > given a name, but you cannot change data by assigning to a name; you can
> > only change what data that name points to. There are certain operations
> > that can be done with mutable objects and immutable objects, but that is
> > another discussion entirely.
> Yes. These two issues often get muddled together in discussions about
> argument passing. Are you avoiding the term "variable"? Some don't
> like to use it because they see Python as somehow fundamentally
> different than other languages. I think the term fits nicely.
In my mind, "variable" implies that the data is implicitly "variable" by
assignment, which is not the case in Python.
Underlying name binding in Python is a Python dictionary; a mapping of
keys to values, where keys are names, and values are pointers to actual
data (there are optimizations for local scopes that replace the
dictionary with a static table, but one could use a dictionary and it
would work the same).
As a result, an assignment in a scope is no more than doing doing an
assignment to a key/value pair in dictionary, with similar "hey, we're
just swapping pointers on assignment, not changing the data itself" (at
least in terms of the 'value' part of the key,value pair).
In a C-semantic pass by reference, you get the pointer to the actual
data, and when you assign to that pointer, you change the data itself.
In a C-semantic pass by value, you get a value with copy on write
If what Python does is like Java, perhaps C# or what have you, great. It
is, however, different from the C semantic description of "pass by value",
"pass by reference", and the standard CS education definition of either.
More information about the Python-list