How to find out the default values of a particular function's argument in another function in Python?

前端 未结 4 1742
野的像风
野的像风 2021-01-12 01:01

Let\'s suppose we have a function like this:

def myFunction(arg1=\'a default value\'):
  pass

We can use introspection to find out the name

4条回答
  •  孤城傲影
    2021-01-12 01:28

    If you define a function f like this:

    >>> def f(a=1, b=True, c="foo"):
    ...     pass
    ...
    

    in Python 2, you can use:

    >>> f.func_defaults
    (1, True, 'foo')
    >>> help(f)
    Help on function f in module __main__:
    f(a=1, b=True, c='foo')
    

    whereas in Python 3, it's:

    >>> f.__defaults__
    (1, True, 'foo')
    >>> help(f)
    Help on function f in module __main__:
    f(a=1, b=True, c='foo')
    

提交回复
热议问题