Home » Python » Note the python function parameters

Note the python function parameters

Python functions have very flexible parameter shapes that can be simple to call and pass into very complex parameters,.


default parameters must be immutable objects, if it is mutable objects, the program will run a logical error!

!

should pay attention to defining the syntax for variable parameters and keyword parameters:


*args is a mutable parameter, and args receives a tuple;


**kw is the keyword parameter, and kW receives a dict.


and how to refer to the syntax of variable arguments and keyword arguments when calling functions:


variable arguments can either be passed in directly: func (1, 2, 3), and you can first assemble list or tuple and then pass in through args: func ( (1, 2, 3));


The

keyword parameter can either be passed in directly: func (a=1, b=2), and you can first assemble dict and then pass in through kW: func ({'a': 1, 'B': '2}'), '.

'

uses *args and **kw as the idiom for Python and, of course, you can use other parameter names, but it's best to use the idiom.


The keyword parameter named by

is to restrict the parameter name the caller can pass in and to provide the default value.


defines the keyword parameter named, do not forget to write the delimiter *, otherwise the definition will be the positional parameter.


reference:

1, http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/ 001431752945034eb82ac80a3e64b9bb4929b16eeed1eb9000


Latest