Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is the default parameter in Python function

Tags:

python

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

def foo(a=[]):
    a.append(3)
    return a

If we call this function using foo(), the output will append integer '3' each time after the call.

When this function is defined, a function object named 'foo' is defined in the current environment, and also the default parameter values are evaluated at this time. Every time when the function is called without a parameter, the evaluated parameter value will be changed according to the code.

My question is, where is this evaluated parameter exist? Is it in the function object or it is in the method object when calling the function? Since everything in python is a object, there must be some place to hold the name->value binding of 'a'-->evaluated parameter. Am I over-thinking this problem?

like image 436
ming.kernel Avatar asked Apr 12 '12 09:04

ming.kernel


People also ask

What is default parameter in Python function?

Default values indicate that the function argument will take that value if no argument value is passed during the function call. The default value is assigned by using the assignment(=) operator of the form keywordname=value.

Where is the default parameter value?

The default parameter is a way to set default values for function parameters a value is no passed in (ie. it is undefined ). In a function, Ii a parameter is not provided, then its value becomes undefined . In this case, the default value that we specify is applied by the compiler.

Where is the parameter in Python?

A parameter is the variable listed inside the parentheses in the function definition. An argument is the value that is sent to the function when it is called.

Where must default arguments be placed in a function definition Python?

The default argument is passed automatically and appears in the output of the function call.


1 Answers

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': []}
>>>
like image 135
yak Avatar answered Nov 08 '22 18:11

yak