Finding the instance reference of an object

Arnaud Delobelle arnodel at googlemail.com
Thu Nov 6 20:44:30 CET 2008


I know this thread has grown quite personal for some of its
participants.  I am posting in a spirit of peace and understanding :)

Joe Strout <joe at strout.net> writes:
[...]
> Um, no, I've admitted that it's a reference all along.  Indeed, that's
> pretty much the whole point: that variables in Python don't contain
> objects, but merely contain references to objects that are actually
> stored somewhere else (i.e. on the heap).  This is explicitly stated
> in the Python docs [1], yet many here seem to want to deny it.

You refer to docs about the *implementation* of Python in C.  This is
irrelevant.

Also, you talk about variables 'containing' something.  In Python,
variables don't contain anything, they're simply names for objects.
'Pass by value' is not relevant to Python as variables do not contain
anything.  'Pass by reference' is not relevant to Python as the language
doesn't have the concept of object reference (in the sense of e.g. C++
reference).

[...]
>> "Calling by value" is not a useful definition of Pythons behaviour.
>
> It really is, though.  You have to know how the formal parameter
> relates to the actual parameter.  Is it a copy of it, or an alias of
> it?  Without knowing that, you don't know what assignments to the
> formal parameter will do, or even what sort of arguments are valid.
> Answer: it's a copy of it.  Assignments don't affect the actual
> parameter at all.  This is exactly what "call by value" means.

Here lies, IMHO, the reason why you think you need Python to 'pass by
value'.  As you believe that variables must contain something, you think
that assignment is about copying the content of a variable.  Assignment
in Python is simply giving a new name to an object.

To understand variables (which I prefer to call 'names') and function
calls in Python you need simply to understand that:

  - a variable is a name for an object
  - assignment is naming an object
  - the parameters of a function are local names for the call arguments
    (I guess 'pass by object' is a good name).

Now quoting the start of your post:

> On Nov 4, 2008, at 12:57 PM, Hendrik van Rooyen wrote:
>>> 5. You see that the first three languages above are passing a
>>> reference by value and using that to mutate and object, yet for
>>> reasons still mysterious, the Python example (which has exactly the
>>> same behavior) must be doing something different.
>>
>> This is dialectic nit picking - WTF makes "passing a reference by
>> value"
>> different from "passing a reference" - the salient point is that its
>> a reference
>> that is passed

I would say that an oject is passed, not a reference.

> I know it seems nit-picky on the surface, but it is important.  It is
> the distinction that lets you answer whether:
>
> def foo(x):
>    x = Foo()
>
> x = Bar()
> foo(x)
>
> ...results in x (after the call) now referring to a Foo, or still
> referring to a Bar.

You don't need this to decide.  This is what happens:

x = Bar() # Call this new Bar object 'x'
foo(x)    # call function foo with argument the object known as 'x'

# Now, in foo:
def foo(x):   # Call 'x' locally the object passed to foo
    x = Foo() # Call 'x' locally this new Foo object.

Obviously after all this, 'x' is still the name of the Bar object
created at the start.


To sum up: for 'pass by value' to make sense in Python you need to
create an unnecessarily complex model of how Python works.  By letting
go of 'pass by value' you can simplify your model of the language
(keeping it correct of course) and it fits in your brain more easily.

Of course your own model is valid but there is a better one which is
easier to grasp for people without a background in C/C++ - like
languages.

-- 
Arnaud



More information about the Python-list mailing list