Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Finding Function Args?

Reply
Thread Tools

Finding Function Args?

 
 
Chris S.
Guest
Posts: n/a
 
      06-15-2004
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.
 
Reply With Quote
 
 
 
 
Mike C. Fletcher
Guest
Posts: n/a
 
      06-15-2004
Check the standard module inspect, particularly;

>>> import inspect
>>> import cgi
>>> inspect.getargspec( cgi.escape )

(['s', 'quote'], None, None, (None,))


HTH,
Mike

Chris S. wrote:

> 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/


 
Reply With Quote
 
 
 
 
Chris S.
Guest
Posts: n/a
 
      06-15-2004
Thanks for the help.

Mike C. Fletcher wrote:

> Check the standard module inspect, particularly;
>
> >>> import inspect
> >>> import cgi
> >>> inspect.getargspec( cgi.escape )

> (['s', 'quote'], None, None, (None,))
>
>
> HTH,
> Mike
>
> Chris S. wrote:
>
>> 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/
>
>

 
Reply With Quote
 
Miki Tebeka
Guest
Posts: n/a
 
      06-15-2004
Hello Mike,

> Check the standard module inspect, particularly;
>
> >>> import inspect
> >>> import cgi
> >>> inspect.getargspec( cgi.escape )

> (['s', 'quote'], None, None, (None,))

or just cgi.escape.func_code.co_varnames

Bye.
--
-------------------------------------------------------------------------
Miki Tebeka <(E-Mail Removed)>
The only difference between children and adults is the price of the toys.

 
Reply With Quote
 
Graham Dumpleton
Guest
Posts: n/a
 
      06-15-2004
"Mike C. Fletcher" <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> Check the standard module inspect, particularly;
>
> >>> import inspect
> >>> import cgi
> >>> inspect.getargspec( cgi.escape )

> (['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.

>>> dir(cgi.escape)

['__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']

>>> dir(cgi.escape.func_code)

['__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.

>>> dir(A.__init__.im_func)

['__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']

>>> dir(A(1).__call__.im_func)

['__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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Finding Server... Finding Host.... enough already!!! Leesa_Tay@softhome.net Computer Support 2 01-20-2006 10:23 AM
Function pointer to void function and int function Giannis Papadopoulos C Programming 5 09-05-2005 09:06 PM
How override ALL function calls? (Is there a "function call function"?) seberino@spawar.navy.mil Python 2 08-01-2005 12:38 PM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
Passing a C++ object's member function to a C function expecing a function pointer! James Vanns C++ 7 01-21-2004 02:39 AM



Advertisments