Created
August 10, 2011 22:48
-
-
Save hugs/1138485 to your computer and use it in GitHub Desktop.
Introspecting a Python function's arguments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ) | |
# 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment