Please help with MemoryError

John Posner jjposner at optimum.net
Fri Feb 12 13:45:46 EST 2010


On 2/12/2010 12:14 PM, Steven D'Aprano wrote:
> On Fri, 12 Feb 2010 06:45:31 -0800, Jeremy wrote:
>
>> You also confirmed what I thought was true that all variables are passed
>> "by reference" so I don't need to worry about the data being copied
>> (unless I do that explicitly).
>
> No, but yes.
>
> No, variables are not passed by reference, but yes, you don't have to
> worry about them being copied.
>
> You have probably been mislead into thinking that there are only two
> calling conventions possible, "pass by value" and "pass by reference".
> That is incorrect. There are many different calling conventions, and
> different groups use the same names to mean radically different things.
>
> If a language passes variables by reference, you can write a "swap"
> function like this:
>
> def swap(a, b):
>      a, b = b, a
>
> x = 1
> y = 2
> swap(x, y)
> assert (x == 2) and (y==1)
>
> But this does not work in Python, and cannot work without trickery. So
> Python absolutely is not "pass by reference".
>
> On the other hand, if a variable is passed by value, then a copy is made
> and you can do this:
>
> def append1(alist):
>      alist.append(1)  # modify the copy
>      return alist
>
> x = []
> newlist = append1(x)
> assert x == []  # The old value still exists.
>
> But this also doesn't work in Python! So Python isn't "pass by value"
> either.
>
> What Python does is called "pass by sharing", or sometimes "pass by
> object reference". It is exactly the same as what (e.g.) Ruby and Java
> do, except that confusingly the Ruby people call it "pass by reference"
> and the Java people call it "pass by value", thus guaranteeing the
> maximum amount of confusion possible.
>
>
> More here:
>      http://effbot.org/zone/call-by-object.htm
>      http://en.wikipedia.org/wiki/Evaluation_strategy
>

Excellent writeup, Steve! You and Jeremy might be interested in a 
message on "pass-by-XXX" from John Zelle, the author of the textbook 
"Python Programming: An Introduction to Computer Science". [1]  This was 
part of a long thread in May 2008 on the Python edu-sig list -- nearly 
as long as "Modifying Class Object", but with nowhere near the fireworks!

Tx,
John

[1] http://mail.python.org/pipermail/edu-sig/2008-May/008583.html




More information about the Python-list mailing list