Finding Function Args?

Discussion in 'Python' started by Chris S., Jun 15, 2004.

  1. Chris S.

    Chris S. Guest

    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.
    Chris S., Jun 15, 2004
    #1
    1. Advertising

  2. 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/
    Mike C. Fletcher, Jun 15, 2004
    #2
    1. Advertising

  3. Chris S.

    Chris S. Guest

    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/
    >
    >
    Chris S., Jun 15, 2004
    #3
  4. Chris S.

    Miki Tebeka Guest

    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 <>
    The only difference between children and adults is the price of the toys.
    Miki Tebeka, Jun 15, 2004
    #4
  5. "Mike C. Fletcher" <> wrote in message news:<>...
    > 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.
    Graham Dumpleton, Jun 15, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ken Varn
    Replies:
    2
    Views:
    612
    Ken Varn
    Jun 22, 2005
  2. Replies:
    3
    Views:
    481
    David Eppstein
    Sep 17, 2003
  3. Pierre Fortin

    args v. *args passed to: os.path.join()

    Pierre Fortin, Sep 18, 2004, in forum: Python
    Replies:
    2
    Views:
    668
    Pierre Fortin
    Sep 18, 2004
  4. er
    Replies:
    2
    Views:
    485
  5. Andrew Tomazos
    Replies:
    5
    Views:
    560
Loading...

Share This Page