Re: Dynamically pass a function arguments from a dict

2005-02-24 Thread Nick Coghlan
Python 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)] on win32 Type help, copyright, credits or license for more information. Running startup script Py import inspect Py help(inspect.getargspec) Help on function getargspec in module inspect: getargspec(func) Get the names and

Re: Dynamically pass a function arguments from a dict

2005-02-24 Thread Scott David Daniels
Mark McEahern wrote: Dan Eloff wrote: How can you determine that func2 will only accept bar and zoo, but not foo and call the function with bar as an argument? Let Python answer the question for you: ... good explanation of how to do this for simple functions. Please be aware the normal way to do

Dynamically pass a function arguments from a dict

2005-02-23 Thread Dan Eloff
You can take a dictionary of key/value pairs and pass it to a function as keyword arguments: def func(foo,bar): print foo, bar args = {'foo':1, 'bar':2} func(**args) will print 1 2 But what if you try passing those arguments to a function def func2(bar,zoo=''): print bar, zoo

Re: Dynamically pass a function arguments from a dict

2005-02-23 Thread Mark McEahern
Dan Eloff wrote: How can you determine that func2 will only accept bar and zoo, but not foo and call the function with bar as an argument? Let Python answer the question for you: def func2(bar='a', zoo='b'): ... pass ... for name in dir(func2): ... print '%s: %s' % (name, getattr(func2, name))

Re: Dynamically pass a function arguments from a dict

2005-02-23 Thread Dan Eloff
Awesome, wrapping that into a function: def getargs(func): numArgs = func.func_code.co_argcount names = func.func_code.co_varnames return names[:numArgs] short, concise, and it works :) variables declared inside the function body are also in co_varnames but after the arguments only it