Parameters passing in Python

Mel Wilson mwilson at the-wire.com
Mon Nov 3 14:19:10 CET 2003


In article <bo58r1$2d28$3 at justice.itsc.cuhk.edu.hk>, kk <k at k.com> wrote:
>When I learnt Java, I was told that for its primitive types, parameter
>passing is done by value. For objects, by reference.
>I started to learn Python months ago, but I still can't figure out the
>parameter passing style of Python. List, Dict, Object...
>When I search in mailing list, I found that someone raised the same
>question and someone responded Python uses 'pass by object'.
>Would anyone explain a bit more in detail?
>Thank you in advance.

   AFAIK when you call a function, the call machinery
creates a namespace for the function to use as it executes.
It finds each of the objects referenced by the actual
parameters and binds that object with the appropriate name
from the function definition.

   This namespace becomes the local namespace for that
invocation of the function.

   A function call like 'int ("42")', for example would go
through a process something like (see the Python Library doc
for the formal parameters of int):

- copying the dictionary that defines int's default arguments,
     {'x':0, 'radix':10}

- updating this copy with the actual parameters from the call,
     {'x':'42'}

- running int's code with the resulting local namespace,
     {'x':'42', 'radix':10}

Or something like that.

   Using a variable in the call

    s = "42"
    int (s)

makes no difference when the actual parameters are inserted
into the namespace.  You can see this in the Python
interpreter:

Python 2.3 (#46, Jul 29 2003, 18:54:32) [MSC v.1200 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> s="42"
>>> print {'x':s}
{'x': '42'}


        Regards.        Mel.




More information about the Python-list mailing list