[Edu-sig] Sticky-note Analogy

John Zelle john.zelle at wartburg.edu
Wed May 14 23:30:12 CEST 2008


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).

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  



More information about the Edu-sig mailing list