Please help with MemoryError

Steve Holden steve at
Fri Feb 12 23:10:01 CET 2010

Antoine Pitrou wrote:
> Le Fri, 12 Feb 2010 17:14:57 +0000, Steven D'Aprano a écrit :
>> 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.
> Well, I think the Ruby people got it right. Python *does* pass parameters 
> by reference. After all, we even speak about reference counting, 
> reference cycles, etc.
> So I'm not sure what distinction you're making here.
He's distinguishing what Python does from the "call by reference" which
has been used since the days of Algol 60.

As has already been pointed out, if Python used call by reference then
the following code would run without raising an AssertionError:

def exchange(a, b):
    a, b = b, a

x = 1
y = 2
exchange(x, y)
assert (x == 2 and y == 1)

Since function-local assignment always takes place in the function
call's local namespace Python does not, and cannot, work like this, and
hence the term "call by reference" is inapplicable to Python's semantics.

Steve Holden           +1 571 484 6266   +1 800 494 3119
PyCon is coming! Atlanta, Feb 2010
Holden Web LLC       

More information about the Python-list mailing list