Learning Python function - parameter type

Function parameters

When you define a Python function can be defined parameter, these parameters in order to determine the specific value in the call when the caller is responsible for the function of the parameter value line arguments passed in

Type of function parameters

First, the location parameter

Also called will pass parameters, the order parameter is the most important and parameters must be clearly provided in the function call! Positional parameters must be in chronological order, one to one, but not too much the number of delivery!

Learning Python function - parameter type

Description: a, b, c is a function of the add line arguments, no specific values, only add (x, y, z) is the value was only passed, a = x = 1, b = y = 2, c = z = 3, wherein x, y, z, called arguments, add (4,5,6,7), add (5,7) are call error

Second, keyword arguments

Call functions, the parameters are passed by value in accordance with the name of the parameter, the parameter defined sequence without passing a parameter called Keyword parameters

Learning Python function - parameter type

Description: the specified when the transfer parameters a = 1, c = 10, b = 3, so that the sequence of these three variables can be passed freely exchange, the add (0, 3, c = 10), both the position parameter and there are keyword parameters, the positional parameters must be placed in front of the keyword parameters set error

Learning Python function - parameter type

Third, the default parameters

In the definition of the function of time, if a default value for a parameter, this parameter becomes the default parameters, the parameters of the position is no longer. When calling the function, we can pass parameters to the default value of a custom, or use the default value.

Learning Python function - parameter type

Description: add (0, 3) does not pass to the value c, the default value of 3 pass

  • The default parameters must be parameter back in position!

    def add(c=2, a, b):此种定义函数的方法是错误的
  • The default parameters try to point to the same object!

Learning Python function - parameter type

Note: The above answer and you may think differently, and why? Should will be created when you define the default function, a variable is a variable, when the body of the function is read into memory, the default parameters of a point to an empty list of objects, a memory address is the same, every function call variable A was added as a

Dynamic parameters

Definition: As the name suggests, is the number of parameters can be changed, so that the call functions can pass a plurality of parameters of any
two methods of dynamic parameters:
1. * args: receiving an asterisk indicates an arbitrary parameter. When called, the actual parameters will be packaged into a tuple incoming form parameters. If you pass a list, the list will be treated as a whole
2 ** kwargs:. Two stars acceptance dynamic parameter key-value pairs, any number. Time of the call will be packaged into actual parameter dictionary
. * Args and ** kwargs parameter is the name of the convention, it can easily be written as any other parameter name matches the naming conventions

* Args: For example child **

def test(*books):
    print(books)
    for i in  books:
        print(i)

books = [ "西游记", "三国演义", "水浒传" ]

test("西游记", "三国演义", "水浒传" )

test(books)

Output

('西游记', '三国演义', '水浒传')
西游记
三国演义
水浒传
(['西游记', '三国演义', '水浒传'],)
['西游记', '三国演义', '水浒传']

Note: The above example is defined in * books a variable parameter, the function call can be transmitted when a plurality of parameters, print (books) can be viewed as a number of parameter tuples, Books list is passed as a whole

Question: Can you make a list inside of it ( 'Journey to the West', 'Three Kingdoms', 'Water Margin') as a parameter passed into respectively

Answer: Of course, when you can, that is, the list of arguments passed to add an asterisk in front

def test(*books):
    print(books)
    for i in  books:
        print(i)

books = [ "西游记", "三国演义", "水浒传" ]
test(books)

result:

(['西游记', '三国演义', '水浒传'])
西游记
三国演义
水浒传

** kwargs: illustration

def func(**kwargs):
    print(type(kwargs))
    for kwg in kwargs:
        print(kwg, kwargs[kwg])

func(k1='v1', k2=[0, 1, 2])

Output:

<class 'dict'>
k1 v1
k2 [0, 1, 2]

Think about what the code is the result:

def func(**kwargs):
    print(type(kwargs))
    for kwg in kwargs:
        print(kwg, kwargs[kwg])

core = {
    "语文": 81,
    "数学": 92

}
func(core)

result:

D:\project\test\venv\Scripts\python.exe D:/project/test/venv/monster.py
Traceback (most recent call last):
  File "D:/project/test/venv/monster.py", line 87, in <module>
    func(core)
TypeError: func() takes 0 positional arguments but 1 was given

Process finished with exit code 1

analysis:

Because ** kwargs need to pass parameter key-value pairs, although the dictionary is the value of key-value pairs, but were transferred separately dictionary when the dictionary will be passed as a parameter position, that there is any way to the dictionary as a multi-way key-value pairs parameter-passing it?

Answer: Add two asterisks in front of the transfer dictionary

func(**core)

The correct result:

<class 'dict'>
语文 81
数学 92

Guess you like

Origin blog.51cto.com/11750513/2426891