Python study notes (5): function parameters

1. Keyword parameters

The parameters passed in according to the position of the formal parameters are called positional parameters; if you use positional parameters to pass in parameter values, you must pass in the parameter values ​​strictly in the order specified when the function is defined.

If the parameter value is passed in according to the parameter name, there is no need to follow the order in which the formal parameters are defined. This method is called a keyword parameter. As shown in the following code:

# 定义一个函数
def girth(width, height) :
    print("width:", width)
    print("height:", height)
    return 2 * (width + height)
# 传统调用函数的方式,根据位置传入参数值
print(girth(3.5, 4.8))

# 根据关键字参数来传入参数值
print(girth(width = 3.5, height = 4.8))

# 使用关键字参数时,可以交换位置
print(girth(height = 4.8, width = 3.5))

# 部分使用关键字参数,部分使用位置参数(关键字参数必须位于位置参数之后)
print(girth(3.5, height = 4.8))

# 下面的代码错误,关键字参数必须位于位置参数之后,关键字参数之后的只能是关键字参数
# print(girth(width = 3.5, 4.8))

2. Parameter default value

In some cases, the program needs to specify a default value for one or more formal parameters when the function is defined-so that when the function is called, the parameter value can be omitted for the formal parameter, and the default value of the formal parameter is used directly . The sample code is as follows:

# 为两个参数指定默认值
def say_hi(name = "allan", message = "欢迎来到毛茸茸小镇") :
    print(name, ",你好")
    print("消息是:", message)
# 全部使用默认参数
say_hi()
# 只有message参数使用默认值
say_hi("crystal")
# 两个参数都不使用默认值
say_hi("crystal", "学习Python")
# 只有name参数使用默认值
say_hi(message = "学习Python")
# 定义一个打印三角形的函数,有默认值的参数必须放在后面
def printTriangle(char, height = 5) :
    for i in range(1, height + 1) :
        # 先打印一排空格
        for j in range(height - i) :
            print(" ", end = "")
        # 再打印一排特殊字符
        for j in range(2 * i - 1) :
            print(char, end = "")
        print()

printTriangle("@", 6)
printTriangle("#", height = 7)
printTriangle("*")

3. Parameter collection (variable number of parameters)

Python allows adding an asterisk (*) in front of a formal parameter, which means that the parameter can accept multiple parameter values, and multiple parameter values ​​are passed in as a tuple.

The essence of parameter collection is a tuple: Python will collect multiple values ​​passed to the books parameter into a tuple. As shown in the following code:

# 定义一个形参个数可变的函数
def test(a, *books) :
    print(books)
    # books被当作元组处理
    for b in books :
        print(b)
    # 输出整数变量a的值
    print(a)
# 调用test()函数
test(5, "Java", "Python")
test()函数的第一个参数是一个个数可变的参数,由于该参数可接收个数不等的参数值,因此如果需要给后面的参数传入参数值,则需要使用关键字参数;否则,程序会把所传入的多个值都当成是传给books参数的。如下代码所示:
# 定义一个形参个数可变的函数
def test(*books, num) :
    print(books)
    for b in books :
        print(b)
    print(num)

# 调用test()函数
test("Java", "Python", num = 5)
Python还可以收集关键字参数,此时Python会将这种关键字参数收集成字典。如下所示:
# 定义一个形参个数可变的函数
def test(x, y, z = 3, *books, **scores) :
    print(x, y, z)
    print(books)
    print(scores)

# 调用test()函数
test(1, 2, 3, "Java", "Python", 语文 = 90, 数学 = 95)

test(1, 2, "Java", "Python", 语文 = 90, 数学 = 95)

test(1, 2, 语文 = 90, 数学 = 95)

4. Reverse parameter collection

The so-called reverse parameter collection refers to the parameters that are passed to the function after their elements are "disassembled" under the premise that the program has objects such as lists, tuples, and dictionaries. As shown in the following code:

# 将列表的元素传递给函数的参数
def test(name, message) :
    print("用户是:", name)
    print("消息是:", message)
my_list = ["crystal", "欢迎来到毛茸茸小镇"]
test(*my_list)

# 将一个元组传递给函数的参数
def test1(name, *nums) :
    print("name参数:", name)
    print("nums参数:", nums)
my_tuple = (1, 2, 3)
# 使用逆向参数收集,将my_tuple元组的元素传给nums参数
test1('allan', *my_tuple)

#使用逆向参数收集,将my_tuple元组的第一个元素传给name参数,剩下的元素传给nums参数
def test2(name, *nums) :
    print("name参数:", name)
    print("nums参数:", nums)
my_tuple = (1, 2, 3)
test2(*my_tuple)

# 如果不使用逆向参数收集(不在元组参数之前添加星号),整个元组将会作为一个参数,而不是将元组的元素作为多个参数
def test3(name, *nums) :
    print("name参数是:", name)
    print("nums参数是:", nums)
my_tuple = (1, 2, 3)
test3(my_tuple)

# 字典也支持逆向参数收集,字典将会以关键字参数的形式传入
def bar(book, price, desc) :
    print(book, "这本书的价格是:", price)
    print("描述信息:", desc)
my_dict = {"book" : "疯狂Python讲义", "price" : 56, "desc" : "这是一本好书!"}
bar(**my_dict)

 

Guess you like

Origin blog.csdn.net/weixin_44679832/article/details/113855471