How to find argument list of a callable?

Discussion in 'Python' started by David Eppstein, Oct 6, 2003.

  1. Suppose I have a callable object x, and suppose for simplicity that it
    doesn't take any *- or **-arguments. How can I get a list of the number
    and names of the arguments of x?

    If x is a function, then apparently this list is in
    x.func_code.co_varnames[:x.func_code.co_argcount]. If x is an old-style
    class, it's in
    x.__init__.func_code.co_varnames[1:x.__init__.func_code.co_argcount].
    If x is an object with a call method, it's in
    x.__call__.func_code.co_varnames[1:x.__call__.func_code.co_argcount].
    If x is a bound method or a new-style class, I have no idea how to find
    the argument names, and for all I know there are several other classes
    of callable that I haven't even thought of.

    So far the only cases I actually need are functions and old-style
    classes, so I have working code, but I have the nagging suspicion that
    there's a cleaner way of doing this that I'm missing.

    For that matter, is there a good way of testing whether x is an
    old-style class? inspect.isclass(x) and not isinstance(x,type) seems to
    work but feels kind of clunky.

    --
    David Eppstein http://www.ics.uci.edu/~eppstein/
    Univ. of California, Irvine, School of Information & Computer Science
     
    David Eppstein, Oct 6, 2003
    #1
    1. Advertising

  2. David Eppstein

    Andrew Dalke Guest

    David Eppstein:
    > Suppose I have a callable object x, and suppose for simplicity that it
    > doesn't take any *- or **-arguments. How can I get a list of the number
    > and names of the arguments of x?


    Take a look at the 'inspect' module.

    >>> import inspect
    >>> def spam(x, y, z=4):

    .... pass
    ....
    >>> inspect.getargspec(spam)

    (['x', 'y', 'z'], None, None, (4,))
    >>>
    >>> def eggs(a, b, *c, **d):

    .... pass
    ....
    >>> inspect.getargspec(eggs)

    (['a', 'b'], 'c', 'd', None)
    >>>


    Andrew
     
    Andrew Dalke, Oct 6, 2003
    #2
    1. Advertising

  3. On Sun, 05 Oct 2003 19:53:43 -0700, David Eppstein
    <> wrote:

    >For that matter, is there a good way of testing whether x is an
    >old-style class? inspect.isclass(x) and not isinstance(x,type) seems to
    >work but feels kind of clunky.


    Don't know if it's enough for what you have in mind but old-style
    classes don't have a __class__ attribute.

    >>> class Test:

    .... pass
    ....
    >>> Test.__class__

    Traceback (most recent call last):
    File "<interactive input>", line 1, in ?
    AttributeError: class Test has no attribute '__class__'
    >>> class Test(object):

    .... pass
    ....
    >>> Test.__class__

    <type 'type'>
    >>>


    With my best regards,
    G. Rodrigues
     
    Gonçalo Rodrigues, Oct 6, 2003
    #3
    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. John Hunter

    get list of callable methods

    John Hunter, Oct 14, 2004, in forum: Python
    Replies:
    1
    Views:
    374
    Lonnie Princehouse
    Oct 14, 2004
  2. Ben Kial
    Replies:
    1
    Views:
    669
    Eric Enright
    Nov 15, 2004
  3. exiquio
    Replies:
    2
    Views:
    548
    exiquio
    Oct 7, 2008
  4. Dave Slayton
    Replies:
    1
    Views:
    195
    Ben Morrow
    Mar 11, 2007
  5. Ulrich Eckhardt
    Replies:
    6
    Views:
    115
    Peter Otten
    Jul 12, 2013
Loading...

Share This Page