passing by refference

Mel Wilson mwilson at the-wire.com
Tue May 13 12:25:33 EDT 2003


In article <mailman.1052835140.9801.python-list at python.org>,
Daan Hoogland <hoogland at astron.nl> wrote:
>is passing by reffence possible in python?

   Everything in Python is passed by reference.  But the
references are frequently references to values.  In these
cases, an assignment works by binding a different value from
the value that was bound before.  With work, a reference to
a mutable object can get you the effect of another
language's "call by reference".  You probably know this, but
you may not have worked out all the consequences.

>I'd like the following code to print
>1
>instead of
>2
>
><file name="D.py">
>def spam(**ni):
>  ni["egg"] = 1;
>
>bacon = 2
>spam(egg = bacon)
>print bacon
></file>

   Here you've created a dictionary object:  {'egg':2} and
bound it with the name "ni" in the namespace for "spam".
You've modified this dictionary to {'egg':1}, but when spam
exits, there are no other references to it and the dictionary
falls out of scope and vanishes.

>the actual construct is going to return several newly created object, to give a
>reason why.

   The Way To return multiple objects is to return multiple
objects:


def spam ():
    return 1, 2, 3

bacon, kippers, fried_bread = spam ()
print bacon


   What you want to do can be done, for the right value of
"what you want to do".

        Regards.        Mel.




More information about the Python-list mailing list