Difference between revisions of "InspectArgs"
Jump to navigation
Jump to search
PeterHarding (talk | contribs) (Created page with 'How to get the args of a function in Python: # "Regular" arguments: >>> import inspect >>> def spam(a, b, c=3): pass ... >>> inspect.getargspec(spam) ArgSpec(args=['a', 'b', 'c'...') |
PeterHarding (talk | contribs) |
||
(2 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
How to get the args of a function in Python: | How to get the args of a function in Python: | ||
=="Regular" arguments== | |||
>>> import inspect | |||
>>> def spam(a, b, c=3): pass | |||
... | |||
>>> inspect.getargspec(spam) | |||
ArgSpec(args=['a', 'b', 'c'], varargs=None, keywords=None, defaults=(3,)) | |||
==Variable and Keyword Args== | |||
# ( http://docs.python.org/faq/programming.html#how-can-i-pass-optional-or-keyword-parameters-from-one-function-to-another ) | # ( http://docs.python.org/faq/programming.html#how-can-i-pass-optional-or-keyword-parameters-from-one-function-to-another ) | ||
===Variable arguments=== | |||
>>> def spamspam(*args): pass | |||
... | >>> def spamspam(*args): pass | ||
>>> inspect.getargspec(spamspam) | ... | ||
ArgSpec(args=[], varargs='args', keywords=None, defaults=None) | >>> inspect.getargspec(spamspam) | ||
ArgSpec(args=[], varargs='args', keywords=None, defaults=None) | |||
===Keyword arguments=== | |||
>>> def spamspamspam(**kwargs): pass | |||
>>> def spamspamspam(**kwargs): pass | ... | ||
... | >>> inspect.getargspec(spamspamspam) | ||
>>> inspect.getargspec(spamspamspam) | ArgSpec(args=[], varargs=None, keywords='kwargs', defaults=None) | ||
ArgSpec(args=[], varargs=None, keywords='kwargs', defaults=None) | |||
[[Category:Python]] | [[Category:Python]] |
Latest revision as of 23:33, 16 September 2011
How to get the args of a function in Python:
"Regular" arguments
>>> import inspect >>> def spam(a, b, c=3): pass ... >>> inspect.getargspec(spam) ArgSpec(args=['a', 'b', 'c'], varargs=None, keywords=None, defaults=(3,))
Variable and Keyword Args
Variable arguments
>>> def spamspam(*args): pass ... >>> inspect.getargspec(spamspam) ArgSpec(args=[], varargs='args', keywords=None, defaults=None)
Keyword arguments
>>> def spamspamspam(**kwargs): pass ... >>> inspect.getargspec(spamspamspam) ArgSpec(args=[], varargs=None, keywords='kwargs', defaults=None)