[Edu-sig] Pass by Reference

John Zelle john.zelle at wartburg.edu
Tue May 20 19:49:56 CEST 2008


Hi All,

OK, really, this is my last post (on this topic).

On Tue, 2008-05-20 at 08:59 -0700, Warren Sande wrote:
> Agreed.  
> 
> Python "behaves like"  call-by-value for immutable types and  "behaves
> like" call-by-reference for mutable types.  Newbies care about how a
> thing behaves, not what's going on behind the scenes.

This is not really an accurate characterization. As I explained in my
previous post, Python never behaves like call-by-reference. Python
always and everywhere (not just in parameter passing) behaves like a
language where variables store references to objects (and happens to
use call-by-value for parameters). I agree that newbies need not even be
engaged in the terminology discussion.

>   Because understanding the behavior lets them write working programs.
> This seemingly inconsistent behavior is one thing that makes teaching
> with Python a bit more difficult.

Here I can't agree at all. The beauty of Python for newbies is that it
offers you one consistent memory model for assignment, parameter
passing, and everything else. This makes it unlike many other languages
that have multiple, often confusingly different mechanisms. I find it
much better as a first language.

>   Beginners want the answer to the question:  "If I pass something to
> a function, can the function change it or not?"  And the answer is,
> "It depends."  That's not a great answer for a beginner.

But again, this has nothing to do with functions AT ALL. The answer to
the question of "can I change it?" Is always and everywhere in Python:
"You can if it's a changeable (mutable) object." This has very little to
do with parameter passing. I'm more and more convinced this is only a
confusion for those of us who cut their teeth with languages that employ
other memory models. Of course, there's no need to confuse (true)
beginners with those other models. They'll only be confused if we
present a confusing picture.

If you're teaching (or writing for) students who might have already
encountered some other languages, then there's some explaining and
comparison to do. But the thing that needs to be explained is that all
"values" are heap-allocated objects, and variables _always_ contain a
reference to one of these. Again, don't confuse this important and cool
difference with notions about how parameters are passed. 

--John
> 
> 
> ----- Original Message ----
> From: John Posner <jjposner at snet.net>
> To: edu-sig at python.org
> Sent: Tuesday, May 20, 2008 10:50:38 AM
> Subject: Re: [Edu-sig] Pass by Reference
> 
> > ... and stop trying to invent new names for a parameter passing
> mechanism
> > that is identical in function to traditional call by value.
> > 
> 
> Yeah, but ... it will be difficult to stick to a call-by-value
> characterization when confronted with this example, which is straight
> from
> "Call by Reference 101":
> 
> def AddArtist(mylist):
>     mylist.append('TheOtherTerry')
> 
> >>> troupe
> ['Graham', 'John', 'Eric', 'Michael', 'Terry']
> 
> >>> AddArtist(troupe)
> 
> >>> troupe
> ['Graham', 'John', 'Eric', 'Michael', 'Terry', 'TheOtherTerry']
> 
> 
> Most students (especially newbies) won't care about what happens under
> the
> hood -- not at first. If it looks like a duck, walks like a duck, and
> quacks
> like a duck ...
> 
> -John
> 
> _______________________________________________
> Edu-sig mailing list
> Edu-sig at python.org
> http://mail.python.org/mailman/listinfo/edu-sig
> 
> 
> 
> _______________________________________________
> 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