Just to muddy the waters even further, David Ranum and I have adopted
the terminology call by assignment for our upcoming CS1 book. we
found this terminology a few Python references and liked it. Here's
an excerpt:
There are many different ways to pass parameters and different
programming languages have chosen to use a variety of them. In Python,
however, all parameters
are passed using a single mechanism known as call by assignment
parameter passing.
Call by assignment parameter passing uses a simple two step process to
pass data when the
function is called, also known as invocation. The first thing that
happens is that the actual parame-
ters are evaluated. This evaluation results in an object reference to
the result. In the first case from
Session 6.11, evaluating a literal number simply returns a reference
to the number itself. In the
second example, evaluating a variable name returns the object
reference named by that variable.
Once the evaluation of the actual parameters is complete, the object
references are passed to
and received by the formal parameters in the function. The formal
parameter becomes a new
name for the reference that is passed. In a sense it is as if we
executed the assignment statement
formal parameter = actual parameter.
Here is Session 6.11: You can fill in the blanks for yourself on the
definition of the hypotenuse function.
>>> hypotenuse(3,4)
5.0
>>>
>>> side1 = 3
>>> side2 = 4
>>> hypotenuse(side1,side2)
5.0
>>>
>>> hypotenuse(side1*2, side2*2)
10.0
Brad
On May 5, 2008, at 5:06 PM, David MacQuigg wrote:
Many thanks to Michael, Anna and John for the very thorough answers
to this question. I especially like John's sticky-note analogy. I
have been using a similar analogy with "labels", but that gets
confused with the other more common uses of the word "label".
Sticky Note is unique and more memorable.
I'm disturbed that there doesn't seem to be any agreement on simple
definitions for "call by reference" and "call by value". A Google
search for [python "call by value"] shows there is widespread
confusion in the Python world. I'm not a computer scientist, but
somewhere I did learn these concepts as being simply copying the
value of a variable vs passing just a reference to the original. I
see that my simple understanding agrees with Kernighan and Ritchie
and with http://en.wikipedia.org/wiki/Call_by_value. (Note that
"call-by-reference", as defined in this article, clearly includes
Python's calling method.)
Luckily, we can answer the question without wading into this swamp.
Here is my latest revision:
'''
Python doesn't need pointers because of the simple relationship
between variables and the objects they refer to. A variable has a
name and a pointer to an object. An object has a type, a value, and
an address in memory. Unlike C, the type is with the object, not
the variable. A variable is "bound" to an object much like a sticky
note on a box. Notes can be moved from one box to another, and a
box can have more than one note, or no notes at all.
Python's pointers are "under the hood", not something the programmer
sees or ever needs to worry about. This part of what makes Python a
higher level language than C. You gain a lot in simplicity and
readability, while losing the ability to work directly with memory
addresses. You also lose a little memory, because objects are more
complex than raw data values.
The sticky-note analogy has a flaw. You can't stick one note on top
of another. When you say x = y = z, all three variables now point
to the object originally pointed to by z. Then when you say y = 8,
y now points to an integer object 8, but x doesn't move with y.
Python's "sticky notes" have a special glue that sticks only to an
object, not to another variable.
See http://effbot.org/zone/python-objects.htm for more on Python
objects and variables.
Note: You can get the address of an object x with the function
id(x), but this is used only to provide a unique identity for the
object, not to access it. If id(x) == id(y), you know that
variables x and y both point to the same object.
'''
-- Dave
_______________________________________________
Edu-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/edu-sig
_______________________________________________
Edu-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/edu-sig