14

Is there a way to introspect a function so that it shows me information on the arguments it takes (like number of args, type if possible, name of arguments if named) and the return value? dir() doesn't seem to do what I want. The __doc__ string sometimes includes method/function arguments, but often doesn't.

tshepang
  • 12,111
  • 21
  • 91
  • 136
mindthief
  • 12,755
  • 14
  • 57
  • 61
  • In general, the documentation (not necessarily `__doc__`) *is* your best bet. –  Dec 12 '10 at 00:46
  • 1
    To see the function signature. Step 1. Read the code. Step 2. Search. http://stackoverflow.com/questions/2677185/how-read-method-signature This is already answered here several times. http://stackoverflow.com/questions/3375573/finding-a-functions-parameters-in-python, also. – S.Lott Dec 12 '10 at 00:49
  • 1
    Appreciate the links, but honestly I think my question is *much* more succinctly and clearly stated then those you've forwarded. This is at least borne out in that I did do a search and it didn't turn up anything after a few minutes of searching. :\ – mindthief Dec 12 '10 at 00:56
  • 2
    @mindthief: interestingly, you accepted an answer completely at odds with what you're asking. You specifically rejected the `__doc__` string in your question, yet accepted it in your answer. It's unclear from that as well as your comments what you're really looking for. Could you elaborate? – S.Lott Dec 12 '10 at 01:25
  • 1
    @S.Lott Sure, the `__doc__` shows *only* the doc string of the function, not the arguments. If you're lucky, then the doc string may explain the use of the arguments and implicitly give the information I was looking for. But the help() function gives you the exact function signature (which was my stated requirement) plus the doc string too, which really is everything you'd need since the doc string may contain additional explanation not obvious from the signature alone. The answer example is actually called on a class not a function, which is why it seems more verbose than you'd expect. – mindthief Dec 12 '10 at 02:22
  • @mindthief: The answer shows help for a class, not a function. I can't see how the answer meets your stated requirement. Further, when I do `help(abs)` it seems to only show the `__doc__` string. But, since you're satisfied that's all that matters. I still don't see how this question isn't a total duplicate. But I guess I'm just being dense. – S.Lott Dec 12 '10 at 15:15
  • 1
    @S.Lott: Define your own function and then try help(the_function) and see what that gives you. Not sure if abs is the best one to try, it might have different behavior being a built-in function. But when I tried that on a function I'd defined, as well as on a function from a third party lib, it gave the complete function sig + doc string. – mindthief Dec 13 '10 at 04:10
  • http://stackoverflow.com/questions/2677185/how-read-method-signature – n611x007 Feb 18 '13 at 14:24

1 Answers1

20

help(the_funcion) should give you all of that information.

Sample:

>>> help(enumerate)
Help on class enumerate in module __builtin__:

class enumerate(object)
 |  enumerate(iterable[, start]) -> iterator for index, value of iterable
 |
 |  Return an enumerate object.  iterable must be another object that supports
 |  iteration.  The enumerate object yields pairs containing a count (from
 |  start, which defaults to zero) and a value yielded by the iterable argument
 |  enumerate is useful for obtaining an indexed list:
 |      (0, seq[0]), (1, seq[1]), (2, seq[2]), ...
 |
 |  Methods defined here:
 |
 |  __getattribute__(...)
 |      x.__getattribute__('name') <==> x.name
 |
 |  __iter__(...)
 |      x.__iter__() <==> iter(x)
 |
 |  next(...)
 |      x.next() -> the next value, or raise StopIteration
 |
 |  ----------------------------------------------------------------------
 |  Data and other attributes defined here:
 |
 |  __new__ = <built-in method __new__ of type object>
 |      T.__new__(S, ...) -> a new object with type S, a subtype of T
Jochen Ritzel
  • 104,512
  • 31
  • 200
  • 194
  • 5
    Worked like a charm, thanks! This other solution from a related question also works: `import inspect print(inspect.getargspec(the_function))` but help() is much better! – mindthief Dec 12 '10 at 01:04
  • I've been working with python for 5+ years now and I didn't know about that one. Fantastic! – Régis B. Mar 10 '16 at 13:17