parsing function parameters

Lee Harr missive at hotmail.com
Wed Aug 3 14:00:03 EDT 2011


>> I am trying to get some information about a function
>> before (and without) calling it.


> how about


def pdict(f):

    parameter_defaults = {}

    defaults = f.func_defaults

    defaultcount = len(defaults)

    argcount = f.func_code.co_argcount

    for i in xrange(f.func_code.co_argcount):

        name = f.func_code.co_varnames[i]

        value = None

        if i >= argcount - defaultcount:

            value = defaults[i - (argcount - defaultcount)]

        parameter_defaults[name] = value

    return parameter_defaults




> No need for the string parameters.
>
> Tim




That's it!



I saw the func_defaults, but could not see how to

make them match up with the co_varnames. I forgot

that keyword args must follow the positional args (duh).



I think this is going to work perfectly.


Thanks to all for the suggestions!


 		 	   		  



More information about the Python-list mailing list