Подтвердить что ты не робот

Как увидеть подпись функции в Python?

Есть ли способ интроспекции функции, чтобы она показывала мне информацию о аргументах, которые она принимает (например, количество аргументов, тип, если это возможно, имя аргументов, если они указаны) и возвращаемое значение? dir(), похоже, не делает того, что я хочу. Строка __doc__ иногда включает аргументы метода/функции, но часто этого не делает.

4b9b3361

Ответ 1

help(the_funcion) должен предоставить вам всю эту информацию.

Пример:

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