Access function defaults

James Stroud jstroud at mbi.ucla.edu
Tue Feb 15 16:46:49 EST 2005


Hello all,

I'm curious--How do I access a function defaults by name?

for example

def bob(wife, wives=[]):
  # add wife to wives if applicable
  if (can_add_wife(wife,wives)):
    wives.append(wife)
  return len(wives)


Say the bob function is adding to wives. What if you want to see what
the wives default is that's being held by the function?

I see you can get at wives with bob.func_defaults, but only if you know
in advance which position it is in in the func_defaults tuple. What if I
want to get at wives by name? bob.wives seems logical but of course
doesn't work. bob.func_code.co_varnames looks like it may work, but
won't because it can't tell you where in func_defaults you you want.
Other than that, I can't seem to find anything.

By the way, I already know a million "better" ways to do this, so no
need for the "why would you want to do that?" responses. I am just
curious about python functions here.

Thanks in advance for any insight.

James
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20050215/89b0798c/attachment.html>


More information about the Python-list mailing list