call by reference howto????

Antoon Pardon apardon at forel.vub.ac.be
Thu Mar 13 04:22:20 EDT 2008


On 2008-02-28, Steven D'Aprano <steve at REMOVE-THIS-cybersource.com.au> wrote:
> On Thu, 28 Feb 2008 02:02:19 +0200, Tamer Higazi wrote:
>
>> Hi!
>> Can somebody of you make me a sample how to define a function based on
>> "call by reference" ???
>
> Python doesn't do call by reference. Nor does it do call by value. Please 
> pay no attention to anyone who says it does.

Whatever python has for a calling convention, it is close enough that
naming it "call by reference" gives people a reasonable idea of what
is going on.

> Instead, read this:
>
> http://effbot.org/zone/call-by-object.htm

Which IMO isn't very helpfull. The mentioning of e.g. mutable just
confuses things since it really isn't the issue.

AFAICS people don't have a problem with understanding the calling
convention of python. They have a problem understanding the
assignment semantics. It is just that these problems of understanding
most often surface when an assignment is made to a parameter which
makes them think they don't understand the calling convention.

The important thing to understand is that an assigment in python
is not copying the contends into the variable but is passing
a reference/binding the name to a new object.

After the variable is assigned to it doesn't refer to the same object
with a new contend. It refers to a different object.

-- 
Antoon Pardon



More information about the Python-list mailing list