How to return a simple variable from a function (still newbie) ?

Carsten Haese carsten at uniqsys.com
Thu Dec 28 11:02:41 EST 2006


On Thu, 2006-12-28 at 15:28 +0000, Paul Hummer wrote:
> You'd have to either pass the variables by reference into the function, [...]

In Python, all function calls are call by reference. Period. The key
difference between Python and other programming languages is in the
behavior of the assignment statement.

To paraphrase http://effbot.org/zone/python-objects.htm :

In C/C++, assignments modify objects by copying the value from the right
hand side into the memory allocated to the object on the left hand side.

In Python, assignments modify namespaces by making the name on the left
hand side become a reference to the object on the right hand side. It is
irrelevant whether the name referred to some other object before the
assignment, and it is irrelevant whether that object is mutable or not.

The only way to achieve call-by-reference-like behavior in the
"assignment modifies objects" sense is by passing a reference to a
mutable object and then invoking the object's methods to mutate it.
Maybe that's what you meant, but that wasn't clear from what you said.

-Carsten





More information about the Python-list mailing list