[Edu-sig] Pass by Reference

Warren Sande warren.sande at rogers.com
Wed May 21 03:35:54 CEST 2008


I would argue that from a "functional" point of view (black box, the way it behaves), Python does behave like call-by-reference, for mutable types.

In another language, if I use call-by-reference, and the function modifies the variable, the caller's variable changes.  That's the same behavior I get in Python with mutable types (just as in John Posner's example).

I understand that it is not truly call-by-reference.  But from the outside, that's what it looks like, for mutable types.  For immutable types it looks like call-by-value, since changing it in the function does not change the caller's "copy" 

I definitely agree that Python is the best choice for a first language.  But there is this one inconsistency (or maybe call it a duality) of mutable vs immutable types, that I think can be a bit confusing.

Putting aside the "pass-by-X" question, a beginner asks:

"If I  do    A = (something)   then   B = A    then I change A, does B also change?"

And the answer, again, is "It depends."  For mutable types, yes, for immutable types, no.  And that's a sticky thing to get into for a beginner who is just learning what variables (names) are about. 

>>> a = 5
>>> b = a
>>> a
5
>>> b
5
>>> a = 7
>>> a
7
>>> b
5

>>> a = [8]
>>> b = a
>>> a
[8]
>>> b
[8]
>>> a.append(9)
>>> a
[8, 9]
>>> b
[8, 9]

I know that in the first case, you are not really "changing" a, you are attaching the name 'a' to a new object (7, instead of 5).  But that's not what it looks like from the outside.  It looks like you are changing a, and b is not changing.  In the second case, you are changing a, and b is changing.

I'm not saying the memory model is inconsistent, just that it can appear that way to a beginner.  And I think that's true whether they have had exposure to another language or not.

Respectfully,
Warren Sande


----- Original Message ----
From: John Zelle <john.zelle at wartburg.edu>
To: Warren Sande <warren.sande at rogers.com>
Cc: edu-sig at python.org
Sent: Tuesday, May 20, 2008 1:49:56 PM
Subject: Re: [Edu-sig] Pass by Reference

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  




-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/edu-sig/attachments/20080520/68e9837b/attachment.htm>


More information about the Edu-sig mailing list