함수를 인트로 스페 츠 (introspect) 할 수있는 방법이있어서 인수의 정보 (가능한 경우 인수의 수, 가능한 경우 유형, 명명 된 경우 인수의 이름과 같은) 및 반환 값이 나와 있습니다. dir()
은 내가 원하는 것을 수행하지 않는 것 같습니다. __doc__
문자열에는 메서드/함수 인수가 포함되는 경우가 있지만 종종 그렇지 않습니다.Python에서 함수 서명을 보는 방법은 무엇입니까?
10
A
답변
15
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
+4
매력처럼 작동했습니다. 감사합니다. 관련 질문의 다른 해결책도 있습니다 : 'import inspect print (inspect.getargspec (the_function))'하지만 help()가 훨씬 낫습니다! – mindthief
+0
나는 파이썬으로 5 년 이상 일해 왔으며 그 사실을 몰랐다. 환상적! –
, 문서 (반드시'__doc__') *이다 * 당신의 최선의 방법. – delnan
기능 서명을 확인하십시오. 1 단계. 코드를 읽습니다. 2 단계. 검색. http://stackoverflow.com/questions/2677185/how-read-method-signature 이미 여러 번 여기에 답변이 있습니다. http://stackoverflow.com/questions/3375573/finding-a-functions-parameters-in-python, 또한. –
링크에 감사하지만 정직하게는 내 질문이 * 많이 * 더 간결하고 분명히 전달한 사람들보다 분명하다고 생각합니다. 적어도 수색을했기 때문에 이것은 수 개월간 검색 한 후에도 아무 것도 나타나지 않았다는 점에서 적어도지지되었습니다. : \ – mindthief