evaluated function defaults: stored where?

Discussion in 'Python' started by David Isaac, May 26, 2005.

  1. David Isaac

    David Isaac Guest

    Default parameter values are evaluated once when the function definition is
    executed.
    Where are they stored? (A guess: in a dictionary local to the function.)
    Where is this documented?

    As a Python newbie I found this behavior quite surprising.
    Is it common in many other languages?
    Is it unsurprising if I look at it right?

    Thanks,
    Alan Isaac
     
    David Isaac, May 26, 2005
    #1
    1. Advertising

  2. [Alan Isaac]
    > Default parameter values are evaluated once when the function definition is
    > executed. Where are they stored?



    >>> def f(a, b=10, c=20):

    .. pass
    >>> f.func_defaults

    (10, 20)


    > Where is this documented?


    http://docs.python.org/ref/types.html#l2h-78



    Raymond Hettinger
     
    Raymond Hettinger, May 26, 2005
    #2
    1. Advertising

  3. David Isaac

    alex23 Guest

    David Isaac wrote:
    > As a Python newbie I found this behavior quite surprising.


    It can be even more surprising if a default value is mutable:

    >>> def foo(a, b=[]):

    .... b.append(a)
    .... return b

    >>> foo(3,[1,2])

    [1, 2, 3]

    >>> foo('c',['a','b'])

    ['a', 'b', 'c']

    >>> foo(1)

    [1]

    So far, everything is behaving much as you'd expect, but then:

    >>> foo(2)

    [1, 2]

    >>> foo(3)

    [1, 2, 3]

    The parameter is bound to the list at creation time and, being mutable,
    is modifiable each time the function is called.

    This can be avoided by modifying your function slightly:

    >>> foo(3, [1,2])

    [1, 2, 3]

    >>> foo('c', ['a','b'])

    ['a', 'b', 'c']

    >>> foo(1)

    [1]

    >>> foo(2)

    [2]

    >>> foo(3)

    [3]

    -alex23
     
    alex23, May 26, 2005
    #3
  4. David Isaac

    alex23 Guest

    I wrote: (some guff on default parameters)

    Of course, it helps if I actually include the alternative function's
    code:

    >>> def foo(a, b = None):

    .... if b == None: b = []
    .... b.append(a)
    .... return b

    Sorry about that :)

    -alex23
     
    alex23, May 26, 2005
    #4
  5. David Isaac

    John Machin Guest

    David Isaac wrote:
    > Default parameter values are evaluated once when the function definition is
    > executed.
    > Where are they stored?


    A good bet for where to start looking for the storage would be as an
    attribute of the function object. From this point, there are two paths:

    (a) Make a function and inspect it:

    >>> def fun(a,b=42,c=666):

    .... pass
    ....
    >>> dir(fun)

    ['__call__', '__class__', '__delattr__', '__dict__', '__doc__',
    '__get__', '__getattribute__', '__hash__', '__init__', '__module__',
    '__name__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',
    '__setattr__', '__str__', 'func_closure', 'func_code', 'func_defa
    ults', 'func_dict', 'func_doc', 'func_globals', 'func_name']
    >>> fun.func_defaults

    (42, 666)
    >>>



    (b) Become familiar with the general structure of the manuals.
    By process of elimination, the Python Reference Manual would be a good
    place to start for a question of this type.
    Inside that manual, this is a likely candidate:
    """
    3.2 The standard type hierarchy
    Below is a list of the types that are built into Python.
    """

    I'll leave you to read further ...

    > (A guess: in a dictionary local to the function.)


    As you've seen the default values are contained in a tuple. So where are
    the keys of the mapping {'b': 42, 'c': 666} that you expected? Suppose
    *you* do the extra research and report back ...

    > Where is this documented?


    See above.

    >
    > As a Python newbie I found this behavior quite surprising.


    Surprisingly good, bad or just different? Do you have alternate
    non-surprising behaviours in mind?

    > Is it common in many other languages?


    That makes two of us who don't know and haven't done any research :)
    My impression based on a limited number of different languages that I've
    worked with or looked at is that Python's apparatus is the best I've
    seen; YMMV.

    > Is it unsurprising if I look at it right?


    Yes; in general this is true across many domains for a very large number
    of referents of "it" :)

    Cheers,
    John
     
    John Machin, May 26, 2005
    #5
  6. > > Is it unsurprising if I look at it right?

    [John Machin's QOTW]
    > Yes; in general this is true across many domains for a very large number
    > of referents of "it" :)


    There's a Quote of the Week in there somewhere.
     
    Raymond Hettinger, May 26, 2005
    #6
  7. David Isaac

    David Isaac Guest

    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?

    Thanks,
    Alan Isaac
     
    David Isaac, May 27, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steph
    Replies:
    2
    Views:
    1,688
    heislord5
    May 11, 2011
  2. Johannes Schaub (litb)

    Function calls that aren't evaluated

    Johannes Schaub (litb), Nov 14, 2010, in forum: C Programming
    Replies:
    4
    Views:
    293
    Tim Rentsch
    Nov 18, 2010
  3. Jason
    Replies:
    2
    Views:
    96
    Jason
    Dec 15, 2005
  4. User1014
    Replies:
    1
    Views:
    198
    Richard Cornford
    Nov 30, 2006
  5. Arturo B
    Replies:
    10
    Views:
    155
Loading...

Share This Page