default values of function parameters

Discussion in 'Python' started by David Isaac, Jun 6, 2005.

  1. 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, Jun 6, 2005
    #1
    1. Advertising

  2. David Isaac

    Terry Reedy Guest

    "David Isaac" <> wrote in message
    news:piPoe.8518$ld3.1868@trnddc04...
    > 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 you think of a function as defining a subclass of execution instances,
    with __init__ inherited from the superclass, then that is a possible way to
    think of them. Goodness depends on your meaning of goodness ;-). Does it
    help you write and use Python functions acurately?

    > If so, are they somehow accessible?
    > How? Under what name?


    This is implementation specific. For CPython, the interactive interpreter
    is your friend. Learn to use it!

    >>> def f(): pass

    ....
    >>> dir(f)

    ['__call__', '__class__', '__delattr__', '__dict__', '__doc__', '__get__',
    '__ge
    tattribute__', '__hash__', '__init__', '__name__', '__new__', '__reduce__',
    '__r
    epr__', '__setattr__', '__str__', 'func_closure', 'func_code',
    'func_defaults',
    'func_dict', 'func_doc', 'func_globals', 'func_name']

    Now give f some parameters with defaults and print f.func_defaults.

    Terry J. Reedy
     
    Terry Reedy, Jun 6, 2005
    #2
    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. BRG
    Replies:
    11
    Views:
    6,395
  2. Jason
    Replies:
    2
    Views:
    514
    Jonathan Mcdougall
    May 13, 2006
  3. desktop
    Replies:
    2
    Views:
    462
    Victor Bazarov
    Jun 6, 2007
  4. Matt Mower
    Replies:
    6
    Views:
    92
    Robert Klemme
    Feb 1, 2005
  5. wyo
    Replies:
    5
    Views:
    112
    scripts.contact
    Apr 28, 2007
Loading...

Share This Page