The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

import-bot (20211) [Avatar] Offline
#1
[Originally posted by thresher88]

I need to query the names and values of a functions keyword arguments. I
suspect that there is an easy way to do this but cannot find it. Would
someone care to make a suggestion?
import-bot (20211) [Avatar] Offline
#2
Re: query function args
[Originally posted by daryl harms]

> I need to query the names and values of a functions keyword arguments. I
> suspect that there is an easy way to do this but cannot find it. Would
> someone care to make a suggestion?

Hi David,

I don't believe there is a way to obtain a functions keyword parameters by
themselves. However, when you first enter a function (i.e. before you've used
any local variables) both the locals() and vars() built-in functions will
return a dictionary whose keys are all the names and whose values are their
settings.

For example:
>>> def f(a,b,c,d,e):
print vars();
print locals();
v = locals();
print v.keys();


>>> f(1,2,e=5,d=4,c=3)
{'d': 4, 'e': 5, 'b': 2, 'c': 3, 'a': 1}
{'d': 4, 'e': 5, 'b': 2, 'c': 3, 'a': 1}
['d', 'e', 'b', 'c', 'a']
>>>

Note, that you should treat these dictionaries as read only.

Daryl