how query function args 
Author Message
 how query function args

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


Tue, 16 Sep 2003 01:45:16 GMT  
 how query function args
I need to query the names and values of a function's keyword arguments.
I
suspect that there is an easy way to do this but cannot find it.  Would
someone care to make a suggestion?


Tue, 16 Sep 2003 01:49:09 GMT  
 how query function args
I need to query the names and values of a function's keyword arguments.
I
suspect that there is an easy way to do this but cannot find it.  Would
someone care to make a suggestion?


Tue, 16 Sep 2003 02:19:07 GMT  
 how query function args

Quote:

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

func.func_defaults will return a tuple of the default values, but not
the names of the formals.


Tue, 16 Sep 2003 02:42:41 GMT  
 how query function args

Quote:

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

This may help - you can pass all arguments as a
dictionary:

def some_function(**dict):
  for key in dict.keys():
    print "%s %s" % (key, dict[key])

some_function(foo='bar', baz='quux', spam='eggs')

this will print:

foo bar
baz quux
spam eggs

If you do argument passing in this way, you can queyr
the arguments all you want and even use keynames as
strings of course.  Is this what you mean?

--
David Allen
http://opop.nols.com/
----------------------------------------
When everything's coming your way, you're in the wrong lane.



Tue, 16 Sep 2003 03:01:06 GMT  
 how query function args

Quote:

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

> func.func_defaults will return a tuple of the default values, but not
> the names of the formals.

func.func_code, the code-object for the function, has most of the
info you want: its co_argcount attribute is the number of arguments
(the ones with defaults are the last len(func.func_defaults) of
them), its co_varnames attribute has the names of local variables
starting with the argument names.

So, the names of arguments which have default values are:
    func.func_code.co_varnames[
        func.func_code.co_argcount - len(func.func_defaults)
            :
        func.func_code.co_argcount
    ]
in the same order as the func.func_defaults.

E.g., if you want name/value tuples (pairs):

def dw(func):
    return zip(
        func.func_code.co_varnames[
            func.func_code.co_argcount - len(func.func_defaults)
                :
            func.func_code.co_argcount
        ], func.func_defaults)

It's of course easy to make this into a dictionary or whatever.

Alex



Tue, 16 Sep 2003 20:57:46 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Title/Subject: Minimize N args function with quasi-newton method with finite difference gradient Minimize N args function with quasi-newton method with finite difference gradient

2. MySQL query in PHP: Not query zero amounts also removes null amounts from query

3. Expanding an array to function args

4. Names of function's args?

5. Default values for function args

6. args(function)

7. passing unknown args to another function

8. Problems passing a function and its args around.

9. args and {args}

10. How do i pass args to another function?

11. Number of args in a function

12. Universal query function. Please help

 

 
Powered by phpBB® Forum Software