Difference between revisions of "InspectArgs"

From PeformIQ Upgrade
Jump to navigation Jump to search
(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'...')
 
Line 2: Line 2:


# "Regular" arguments:
# "Regular" arguments:
>>> import inspect
 
>>> def spam(a, b, c=3): pass
>>> import inspect
...
>>> def spam(a, b, c=3): pass
>>> inspect.getargspec(spam)
...
ArgSpec(args=['a', 'b', 'c'], varargs=None, keywords=None, defaults=(3,))
>>> inspect.getargspec(spam)
ArgSpec(args=['a', 'b', 'c'], varargs=None, keywords=None, defaults=(3,))




Line 13: Line 14:


# Variable arguments:
# 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:
# Keyword arguments:
>>> def spamspamspam(**kwargs): pass
 
...
>>> def spamspamspam(**kwargs): pass
>>> inspect.getargspec(spamspamspam)
...
ArgSpec(args=[], varargs=None, keywords='kwargs', defaults=None)
>>> inspect.getargspec(spamspamspam)
ArgSpec(args=[], varargs=None, keywords='kwargs', defaults=None)


[[Category:Python]]
[[Category:Python]]

Revision as of 23:31, 16 September 2011

How to get the args of a function in Python:

  1. "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,))


  1. Variable and Keyword Args:
  2. ( http://docs.python.org/faq/programming.html#how-can-i-pass-optional-or-keyword-parameters-from-one-function-to-another )
  1. Variable arguments:
>>> def spamspam(*args): pass
...
>>> inspect.getargspec(spamspam)
ArgSpec(args=[], varargs='args', keywords=None, defaults=None)


  1. Keyword arguments:
>>> def spamspamspam(**kwargs): pass
...
>>> inspect.getargspec(spamspamspam)
ArgSpec(args=[], varargs=None, keywords='kwargs', defaults=None)