2010-12-12 15 views
10

Czy istnieje sposób introspekcji funkcji, aby wyświetlał mi ona informacje o argumentach, które bierze (np. Liczba argumentów, typ, jeśli to możliwe, nazwa argumentów, jeśli jest nazwana) i wartość zwracana? dir() wydaje się nie robić tego, co chcę. Łańcuch __doc__ czasami zawiera argumenty metody/funkcji, ale często nie.Jak wyświetlić podpis funkcji w Pythonie?

+0

Generalnie, dokumentacja (niekoniecznie '__doc__') * to * najlepszym. – delnan

+1

Aby zobaczyć podpis funkcji. Krok 1. Przeczytaj kod. Krok 2. Wyszukaj. http://stackoverflow.com/questions/2677185/how-read-method-signature Odpowiedź na to pytanie już kilka razy. http://stackoverflow.com/questions/3375573/finding-a-functions-parameters-in-python, także. –

+1

Doceń linki, ale szczerze myślę, że moje pytanie jest * dużo * bardziej zwięzłe i jasno określone niż te, które przekazałeś. Jest to co najmniej potwierdzone tym, że przeszukałem i nic się nie pojawiło po kilku minutach poszukiwań. : \ – mindthief

Odpowiedz

15

help(the_funcion) powinien podać wszystkie te informacje.

Próbka:

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

Pracował jak urok, dzięki! To inne rozwiązanie z pokrewnego pytania działa również: 'import inspect print (inspect.getargspec (the_function))' ale help() jest znacznie lepszy! – mindthief

+0

Pracuję z Pythonem od 5 lat i nie wiedziałem o tym. Fantastyczny! –

Powiązane problemy