[Edu-sig] Sticky-note Analogy

kirby urner kirby.urner at gmail.com
Thu May 15 03:42:34 CEST 2008


On Wed, May 14, 2008 at 2:30 PM, John Zelle <john.zelle at wartburg.edu> wrote:

> At some point, I have to just let this go, as I think we all on this
> list have a pretty good understanding of the differences between C and
> Python in terms of assignment and parameter passing. But let's _not_ use
> the term "pass by reference" when talking about Python. You CANNOT
> CHANGE THE CONTENTS OF THE VARIABLE THAT IS SUPPLIED AS AN ACTUAL
> PARAMETER. It will still refer to the same object (contain the same
> reference) regardless of what is done to the formal parameter. Hence,
> the variable IS NOT passed by reference. The value of the variable
> (which happens to be a reference) is copied. Pass by value is the
> accepted label for this mechanism. Pass by reference means something
> else (as I've pointed out in previous posts).
>

This seems an especially clear statement.

Comparing two programs, one C and one Python:

C:

#include <stdio.h>

void f (char* r){
	*r = 'b';
}

int main() {
	char p = 'a';
	char* s = &p;
	printf("p = %c\n", p);
	f(s);
	printf("p = %c\n", p);
}

kirby at dell:~$ cc testing.c
kirby at dell:~$ ./a.out
p = a
p = b

Python:

IDLE 1.2.2
>>> def f(r): r = 'b'

>>> def main():
	p = 'a'
	print p
	f (p)
	print p

	
>>> main()
a
a

We say copy-by-value because the local variable has its own identity
and rebinding it to another object within f isn't going to affect the
corresponding variable in the caller (as John was just mentioning).

In the C example, *r is essentially identical to *p, hence pass-by-reference.

The fact that the local and calling variables might share references
to a mutable object is obscuring the fact that these aren't two names
for the same door, as in C, but two separate doors to the same object.

Kirby

> Christopher's point on teaching the differences directly is, I think, a
> good one. But we don't need to worry about that for beginners.
>
> --John
>
>
>
> On Tue, 2008-05-13 at 22:10 +0100, Christopher Thoday wrote:
>> Would it not be better to describe the differences between C and Python
>> variables directly rather than using an analogy?
>>
>> In C, a variable is the address of a storage location that contains its
>> value. If that value is itself an address then the variable is described
>> as a pointer.
>>
>> In Python, a variable is a reference to an object that has a type as
>> well as a value.
>>
>> The statement:
>>
>>     a= b = c;
>>
>> results in three separate values in C but only one in Python.  The
>> effect is the same in both languages provided that c refers to a
>> constant. However, if c is a mutable object, such as a list, then
>> changing the value of one variable changes them all. Anyone coming to
>> Python from C may be confused by this as it is not clearly described in
>> any of the books on Python that I have read.
>>
>> When passing an argument to a function, C uses pass-by-value whereas
>> Python uses what is in effect pass-by-reference. In order to obtain
>> pass-by-reference in C the value must be a pointer. Inside the function
>> the pointer must be dereferenced by prefixing it with an asterisk to
>> obtain the value.
>>
>> Eur Ing Christopher Thoday
>> Software Engineer
>>
>> _______________________________________________
>> Edu-sig mailing list
>> Edu-sig at python.org
>> http://mail.python.org/mailman/listinfo/edu-sig
>>
> --
> John M. Zelle, Ph.D.             Wartburg College
> Professor of Computer Science    Waverly, IA
> john.zelle at wartburg.edu          (319) 352-8360
>
> _______________________________________________
> Edu-sig mailing list
> Edu-sig at python.org
> http://mail.python.org/mailman/listinfo/edu-sig
>


More information about the Edu-sig mailing list