Where is the default parameter in Python function

后端 未结 5 1548
情深已故
情深已故 2021-01-05 08:52

I think many people have seen the python\'s function which receives default parameters. For example:

def foo(a=[]):
    a.append(3)
    return a
相关标签:
5条回答
  • 2021-01-05 09:19

    It's attached to the function object, see foo.func_defaults:

    >>> foo()
    >>> foo.func_defaults
    ([3],)
    >>> foo()
    >>> foo.func_defaults
    ([3, 3],)
    

    In case if you want to get the mapping of a onto [], you can access foo.func_code:

    defaults = foo.func_defaults
    # the args are locals in the beginning:
    args = foo.func_code.co_varnames[:foo.func_code.co_argcount] 
    def_args = args[-len(defaults):]  # the args with defaults are in the end
    print dict(zip(def_args, defaults)) # {'a': []}
    

    (But, apparently, the yak's version is better.)

    0 讨论(0)
  • 2021-01-05 09:20

    It is stored in the func_defaults attribute of the function object.

    >>> foo.func_defaults
    ([],)
    >>> foo()
    ([3],)
    
    0 讨论(0)
  • 2021-01-05 09:23

    As others already said, the default values are stored in the function object.

    For example, in CPython you can do this:

    >>> def f(a=[]):
    ...     pass
    ...
    >>> f.func_defaults
    ([],)
    >>> f.func_code.co_varnames
    ('a',)
    >>>
    

    However, co_varnames may contain more than names of args so it needs further processing and these attributes might not even be there in other Python implementations. Therefore you should use the inspect module instead which takes care of all implementation details for you:

    >>> import inspect
    >>> spec = inspect.getargspec(f)
    >>> spec
    ArgSpec(args=['a'], varargs=None, keywords=None, defaults=([],))
    >>>
    

    The ArgSpec is a named tuple so you can access all values as attributes:

    >>> spec.args
    ['a']
    >>> spec.defaults
    ([],)
    >>>
    

    As the documentation says, the defaults tuple always corresponds to the n last arguments from args. This gives you your mapping.

    To create a dict you could do this:

    >>> dict(zip(spec.args[-len(spec.defaults):], spec.defaults))
    {'a': []}
    >>>
    
    0 讨论(0)
  • 2021-01-05 09:27

    I have found an interesting situation: in python 2.5.2 version, try the function 'foo()'

    >>> foo()
    [1]
    >>> foo()
    [1]
    >>> foo()
    [1]
    

    Because the objects of the function called are different:

    >>> id(foo())
    4336826757314657360
    >>> id(foo())
    4336826757314657008
    >>> id(foo())
    4336826757314683160
    

    In 2.7.2 version:

    >>> foo()
    [1]
    >>> foo()
    [1, 1]
    >>> foo()
    [1, 1, 1]
    

    In this case, the object is the same each time calling the function:

    >>> id(foo())
    29250192
    >>> id(foo())
    29250192
    >>> id(foo())
    29250192
    

    Is it a problem of different versions?

    0 讨论(0)
  • 2021-01-05 09:28

    It's in the function object, in the func_defaults:

    def f(a=[]): a.append(3)
    
    print f.func_defaults # ([],)
    
    f()
    
    print f.func_defaults # ([3],)
    
    0 讨论(0)
提交回复
热议问题