On Sat, 11 Dec 2004 12:43:01 GMT, "houbahop" <d.lapasset[Remove
me)@chello.fr> declaimed the following in comp.lang.python:
> Thank you everyone, but I still not understand why such a comon feature like
> passing parameters byref that is present in most serious programming
> languages is not possible in a clean way,here in python.
>
Well, in C, everything is also passed by value. The programmer
has to explicitly pass an "address as the value" instead of the actual
value if they want to change the contents. And, of course, the function
has to know that it is an address, and explicitly dereference the
address to gain access to the actual content value.
Using my overworked and not quite complete example...
Most languages variables can be described as named boxes (think
of a post office sorting rack -- each "person/address" has a separate
box.
In those languages, "A = B" means finding the BOX with belonging
to "B" (has "B" on the front of the box), COPYING what is inside it, and
then putting that COPY into the box belonging to "A" (taking out and
discarding whatever might have been in "A"'s box).
In Python, you still have those boxes (objects), but the label
(person/address) is not engraved on the box. Instead, it is on a
"Post-it" note. "A = B" means finding the "Post-it" note -- where ever
it is, since it isn't a fixed location -- and putting it on the box that
also has the "B" "Post-it" note. If the "A" was the last "Post-it" note
on the original box, the box contents are thrown away, and the box is
available for reuse. You now have ONE BOX with TWO "Post-it"s, "A" and
"B".
When you call a function, passing "A", for example, you create a
third "Post-it" note, with the function's dummy argument name "D", and
attach it to the same box. The box now has three "Post-it"s attached;
two from the outer scope, and one from the function scope.
An assignment to "D" moves the "Post-it" from that shared box,
to whatever box contains the value of the assignment. And when the
function exits, the clean-up code goes through and finds all the
"Post-it"s that belong to the function, and deletes the "Post-it" (the
contents of the box only get deleted if no other "Post-it" is attached).
The closest equivalent to the C, that I can imagine at the
moment, is to package your arguments into a list before the call, invoke
the function passing the list -- and access the arguments by list
subscripts, followed by extracting the list elements back into the
original "names" after the call returns... Untested:
args = [ a1, a2, a3 ]
function(args)
(a1, a2, a3) = tuple(args)
However, 'tis probably cleaner to just use a tuple return from
the function...
(a1, a2, a3) = function(a1, a2, a3)
Where the function can perform internal modifications to the
arguments passed in, and then returns ALL of them at the end. Other
languages can only return a single entity, Python can return the
"world"... (... return globals() <G>)
--
> ================================================== ============ <
> | Wulfraed Dennis Lee Bieber KD6MOG <
> | Bestiaria Support Staff <
> ================================================== ============ <
> Home Page: <http://www.dm.net/~wulfraed/> <
> Overflow Page: <http://wlfraed.home.netcom.com/> <