Modifying Class Object
steven at REMOVE.THIS.cybersource.com.au
Tue Feb 9 06:38:26 CET 2010
On Tue, 09 Feb 2010 05:01:16 +0100, Alf P. Steinbach wrote:
> * Stephen Hansen -> Alf P. Steinbach:
>> To say, "pass by value" implies things to people. It describes a sort
>> of world where I'm a function about to do some work, and on my desk I
>> have a series of boxes with names on it. It describes an environment
>> where someone comes over and drops something into each of my boxes. The
>> contents of these boxes are mine alone!
> Then, when the imprecision makes people misunderstand, one should not
> say that.
> One should then be more precise.
> One should say /what/ is passed by value.
I don't see how that matters. Python's behaviour is EXACTLY the same, no
matter what you pass to the function.
You don't pass pointers in Python, because you, the Python author, have
no way of specifying a pointer in Python code. It is *always* an object
that you pass, by giving a literal, the name an object is bound to, or
some other expression that returns an object:
function( my_object )
function( 12345 )
function( ham(3) + spam(5) )
If you're talking to the person who writes Python code, then you should
discuss the sorts of things that exist in Python: objects and names and
expressions, but no pointers.
If you're talking about the Python virtual machine, instead of the high-
level Python code, then you should say so -- but there are still no
pointers in the VM. Look at the output of dis.dis and you will see
objects pushed onto a stack, but no pointers.
It's not until you get past the level of Python code, past the virtual
machine, and into the implementation of the virtual machine, that you
will finally find pointers.
But why stop there? If somebody asks you a question about Python, and you
choose to ignore them and answer a different question about one
particular implementation's internals instead, I don't see why you stop
there. Why not go down another layer and talk about copying bytes, or two
layers and answer that Python does call-by-bit-flipping, or a layer below
that and say it's all done by varying potential differences?
You shouldn't expect your listener to understand machine code to
understand Python's high-level behaviour, nor should you expect them to
be C coders. You don't need to understand pointers to understand a
language that doesn't support them.
> The terms apply very well to Java. And Java has the identical parameter
> passing mechanism for class type objects. Hence, the argument is bogus.
Everything I've said applies to Java as well, and the argument about call-
by-whatever is just as prevalent in Java circles as in Python.
Q: If Java uses the pass-by reference, why won't a swap
A: Your question demonstrates a common error made by Java
language newcomers. Indeed, even seasoned veterans find
it difficult to keep the terms straight.
Java does manipulate objects by reference, and all
object variables are references. However, Java doesn't
pass method arguments by reference; it passes them by
Myth: "Objects are passed by reference, primitives are
passed by value"
Some proponents of this then say, "Ah, except for immutable
objects which are passed by value [etc]" which introduces
loads of rules without really tackling how Java works.
Java is Pass-by-Value, Dammit!
Java programmers suffer for a bad mental model just as much as Python
All this is because of two conceptual mistakes: (1) the Java community
has conflated the internals of what happens in the implementation of the
Java VM with high-level Java code; and (2) a refusal to accept that there
are calling strategies other than pass-by-value and pass-by-reference.
More information about the Python-list