Finding Function Args?

C

Chris S.

Is there a way to dynamically find out how many arguments a function
requires? I thought this would be listed somewhere with dir() but I
can't find anything. Any help is appreciated.
 
M

Mike C. Fletcher

Check the standard module inspect, particularly;
(['s', 'quote'], None, None, (None,))


HTH,
Mike
Is there a way to dynamically find out how many arguments a function
requires? I thought this would be listed somewhere with dir() but I
can't find anything. Any help is appreciated.

________________________________________________
Mike C. Fletcher
Designer, VR Plumber, Coder
http://members.rogers.com/mcfletch/
blog: http://zope.vex.net/~mcfletch/plumbing/
 
C

Chris S.

Thanks for the help.
Check the standard module inspect, particularly;
(['s', 'quote'], None, None, (None,))


HTH,
Mike
Is there a way to dynamically find out how many arguments a function
requires? I thought this would be listed somewhere with dir() but I
can't find anything. Any help is appreciated.


________________________________________________
Mike C. Fletcher
Designer, VR Plumber, Coder
http://members.rogers.com/mcfletch/
blog: http://zope.vex.net/~mcfletch/plumbing/
 
G

Graham Dumpleton

Mike C. Fletcher said:
Check the standard module inspect, particularly;
(['s', 'quote'], None, None, (None,))

Also:

cgi.escape.func_code.co_argcount

Should yield:

2

Lots of func stuff should be visible when dir() is used. You just
need to work out what is where.
['__call__', '__class__', '__delattr__', '__dict__', '__doc__',
'__get__', '__getattribute__', '__hash__', '__init__', '__module__',
'__name__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',
'__setattr__', '__str__', 'func_closure', 'func_code',
'func_defaults', 'func_dict', 'func_doc', 'func_globals', 'func_name']
['__class__', '__cmp__', '__delattr__', '__doc__', '__getattribute__',
'__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__',
'__repr__', '__setattr__', '__str__', 'co_argcount', 'co_cellvars',
'co_code', 'co_consts', 'co_filename', 'co_firstlineno', 'co_flags',
'co_freevars', 'co_lnotab', 'co_name', 'co_names', 'co_nlocals',
'co_stacksize', 'co_varnames']

Similar thing for class methods and callable objects if you know where
to look.
['__call__', '__class__', '__delattr__', '__dict__', '__doc__',
'__get__', '__getattribute__', '__hash__', '__init__', '__module__',
'__name__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',
'__setattr__', '__str__', 'func_closure', 'func_code',
'func_defaults', 'func_dict', 'func_doc', 'func_globals', 'func_name']
['__call__', '__class__', '__delattr__', '__dict__', '__doc__',
'__get__', '__getattribute__', '__hash__', '__init__', '__module__',
'__name__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',
'__setattr__', '__str__', 'func_closure', 'func_code',
'func_defaults', 'func_dict', 'func_doc', 'func_globals', 'func_name']

Obviously, the "inspect" module is mean't to hide to a degree all this
stuff, but still sometimes easier to access it directly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top