Reference: www.liaoxuefeng.com/wiki/101695…

Function definition and call

Def function identifier (parameter): function body content return [content, no return value defined, return None]Copy the code
Def test(): pass def test1(): return 1 # x def test2(x): Return 2 x * * def test3 () return 1, 2, a = test () print (a) # print None b = test1 () print (b) print # 1 # test (2) c = 2 is arguments to test2 (2) D,e = test3() print(d,e) # d=1,e =2Copy the code

Function and the cords

By reference

  • Positional parameter transfer: The parameter is transferred strictly according to the position of the parameter. The number of parameters and arguments must correspond
Def test(x, y, z): print(x,y) test(1,2) : def test(x, y, z): print(x,y) test(1,2)Copy the code
  • Keyword pass-by: The call function passes parameters with the parameter name, which can be in a different order than the parameters
def test(x, y, z):
    print(x, y, z)


test(z=3, y=2, x=1)
Copy the code
  • Note: The location parameter must be passed before the keyword parameter
def test(x, y, z): Print (x, y, z) test (1, 2, 3) test (z = 3, y = 2, x = 1) test (y = 2, x = 1, z = 3) test (y = 1, 2, z = 3) test (x = 1, 2, 3) # errorCopy the code
  • Conclusion: There are two parameter transmission modes: 1. Parameter transmission by location 2. Parameter transmission by keyword. Positional parameters must carry the parameter name, which may not correspond to the parameter. Positional parameters must precede keyword parameters

The parameter types

Python function parameters are complex and need to be understood to work well

Parameter defaults (default parameters)

When Python defines a function, you can set default values for parameters that the caller can pass away and use to simplify the function call

def add(x=1, y=2): print(x + y) def add2(x=2,y): Print (x+y) add() # print(x+y) add() # print(x+y) add() # print(x =1,y=2 add(3, 4) add(y=3, x=4) add(3, y=5) add(x=6, 10) Remember that positional arguments must precede keyword argumentsCopy the code

Variable position parameter

Allows the caller to pass in 0 to n positional arguments preceded by *

def add(iterable):
    num = 0
    for i in iterable:
        num += i
    print(num)


def add2(*nums):
    num = 0
    for i in nums:
        num += i
    print(num)


add([x for x in range(101)])
add2(1, 3, 4, 5, 6)
Copy the code

When the parameter is not *, the parameter passed must be an iterable object. With *, any value can be passed, and NUMS encapsulates the parameter as an ancestor

Variable keyword arguments

Allows the caller to pass in 0 to n keyword arguments preceded by **

def test(**kwargs):
    for k, v in kwargs.items():
        print(k, v)


test(a=1, b=2, c=3)
Copy the code
  • Note: The variable position argument must precede the variable keyword argument

A mixture of

Def showConfig(a, b, *args, **kwargs): def showConfig(a, b, *args, **kwargs): Pass def showConfig(a, b, **kwargs, **kwargs): The variable keyword argument must pass after the variable position argumentCopy the code
def fn(x, y, *args, **kwargs): Print (x, y, args, kwargs) fn(1, 2, 3, 5, a=1, b=2) fn(1, 2, 2, a=1, b=2) fn(1, 2, 2, a=1, b=2) fn(1, 2, 2, a=1, b=2) Y = 2, z = 3) # z was kwargs received fn (x = 1, y = 2, 1, 2) # location parameter must be in front of the keyword arguments fn (1, 2, x = 3, y = 4) # got multiple values for argumentCopy the code

Keyword – only parameter

Python3 adds the keyword-only argument, a parameter defined after an * or variable position argument. The parameter can only be passed by keyword argument

Def f (*args, a, b): print(args, a, b): def f (*args, a, b): print(args, a, b): Print (args, a, b) fn(1, 2, 3, a=4, b=4) fn(a=4, b=4) fn(a=4, b=4) fn(1, 2, 3, 4, 5) missing 2 required keyword-only arguments: 'a' and 'b' fn2()Copy the code
  • If the variable position argument above is not needed, it can be shortened to *
def fn3(*, a, b=1):
    print(a, b)
    
fn3(a=3)
Copy the code

Positional read-only parameter

After python3.8, add position-only parameters. All parameters before/are position-only parameters

Combine all parameter types

  • A and B can only pass positional parameters. An error will be reported if a= or b=

  • C and D can be transmitted by location or keyword

  • E and f can be transmitted only by keyword

Parameters of deconstruction

Use * or ** to construct an iterable, taking all values as arguments to the function

def add(*nums): num = 0 for i in nums: num += i print(num) def fn(*args): for i in args: print(i) l = [ x for x in range(101) ] d = {"a": 1, "b":2, "c":3} add(*d.values()) add(l) #Copy the code
def fn(**kwargs):
    for k, v in kwargs.items():
        print(k, v)


d = {"a": 1, "b": 2, "c": 3}
fn(**d)

Copy the code