evaluated function defaults: stored where?

Jp Calderone exarkun at divmod.com
Fri May 27 17:13:22 EDT 2005


On Fri, 27 May 2005 21:07:56 GMT, David Isaac <aisaac0 at verizon.net> wrote:
>Alan Isaac wrote:
>> Default parameter values are evaluated once when the function definition
>is
>> executed. Where are they stored? ... Where is this documented?
>
>Forgive any poor phrasing: I'm not a computer science type.
>At http://www.network-theory.co.uk/docs/pytut/tut_26.html we read:
>"The execution of a function introduces a new symbol table
>used for the local variables of the function. More precisely,
>all variable assignments in a function store the value in the local
>symbol table; whereas variable references first look in the local
>symbol table, then in the global symbol table, and then in the table of
>built-in names."
>
>But the default values of function parameters seem rather like a static
>attributes of a class.
>Is that a good way to think of them?
>If so, are they somehow accessible?
>How? Under what name?
>

The inspect module will get them for you:

    exarkun at boson:~$ python
    Python 2.4.1 (#2, Mar 30 2005, 21:51:10) 
    [GCC 3.3.5 (Debian 1:3.3.5-8ubuntu2)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> def f(x='y'): pass
    ... 
    >>> import inspect
    >>> inspect.getargspec(f)
    (['x'], None, None, ('y',))
    >>> 

As to where they are actually stored, this should be considered an implementation detail, but you can look at inspect.py to see how it pulls the values out (they're just in an attribute on the function object).

Jp



More information about the Python-list mailing list