Python function foundation ten: positional parameters and keyword parameters

  1. Positional and keyword parameters

The actual parameters are divided into positional parameters and keyword parameters according to the different transfer methods.
1) When the positional parameters
call the function, let the actual parameters and the formal parameters correspond to each other (the first actual parameter is assigned to the first formal parameter, and the second Assign value to the second formal parameter with one actual parameter...)
Format: Data 1, Data 2, Data 3,…

位置参数
func1(10, 20, 30)       # x:10, y:20, z:30
func1(10, 30, 20)       # x:10, y:30, z:20

2) Keyword parameters
Let actual parameters and formal parameters correspond to each other by keywords (formal parameter names)
Format: formal parameter name 1=data 1, formal parameter name 2=data 2,...

关键字参数
func1(x=100, y=200, z=300)   # x:100, y:200, z:300
func1(z=30, x=10, y=20)     # x:10, y:20, z:30
# func1(x=100, y=200, y=300)    # 报错!

3) Mixed
use of key parameters and positional parameters . When mixed, positional parameters must be in front of keyword parameters

*Parameters means that there can be multiple parameters

混用
func1(10, z=30, y=20)    # x:10, y:20, z:30
# func1(x=20, 40, z=60)   # 抱错!位置参数必须在关键字参数的前面

print(10, end='a')
print(10, 20, 30, sep=',')

def print(*data, end='\n', sep=' '):
    pass
sorted([1, 23, 8], reverse=True)
  1. Parameter default value When
    defining a function, you can assign a default value to the parameter in the form of'formal parameter name=data'. When calling the function, the parameter with default value can not be assigned.
    Note: The parameter with default value must be in the parameter without default value. Behind
def func2(x=1, y=10, z=100):
    print(f'x:{x}, y:{y}, z:{z}')


func2(1, 2, 3)    # x:1, y:2, z:3
func2(10, 20)    # x:10, y:20, z:100
func2(10)
func2()

func2(y=200)
func2(z=300)

3. Parameter type description When
defining a function, you can explain the parameter type

  1. Assign a default value, what type is the default value, and what type of parameter is the description
  2. Formal parameter name: data type
def func5(x: list) -> None:
    x.append('100')
func5([])

4. Variable length parameter
Add or add **
before the formal parameter to make this parameter a variable length parameter. The formal parameter of the variable length parameter can accept multiple actual parameters at the same time 1) The variable length parameter
with * will become a tuple, and the elements in the tuple are the corresponding actual parameters

Note:. A with a function may be present simultaneously and without parameters, if without the band behind the parameters must be used without the * key parameter
. B belt / * parameter must use position parameter

2) Variable length parameters
with /** Variable length parameters with /** will become a dictionary; when calling, use keyword parameters to pass parameters, each keyword is the key of the dictionary, and the data behind the keyword is the dictionary Value of

Note: a. When defining, the fixed-length parameter must be placed before the /** variable-length parameter.
b. The variable-length parameter with /* and /** can exist at the same time, but /* must be before /**. (If it exists at the same time, the function can be more flexible when calling)

def func6(*x):
    print(f'x:{x}')


func6()
func6(10)
func6(10, 20)
func6(1, 2, 3, 4, 5)
# 练习:定义一个函数,可以求多个数的和
# sum1(10, 20)  sum1(10, 20, 30)
def sum1(*num):
    s = 0
    for x in num:
        s += x
    print(s)

def func10(x, **y):
    print(f'x:{x}, y:{y}')


func10(10, a=20, c=30, d=40)    # x:10, y:{'a': 20, 'c': 30, 'd': 40}
func10(a=1, b=2, c=3, x=100)    # x:100, y:{'a': 1, 'b': 2, 'c': 3}

def func8(*x, y):
    print(f'x:{x}, y:{y}')


func8(1, 2, 3, y=10)

Guess you like

Origin blog.csdn.net/SaharaLater/article/details/111564450