Python允许函数参数具有默认值。如果在没有参数的情况下调用函数,参数将获得其默认值。
默认参数:
Python有一种不同的方式来表示函数参数的语法和默认值。默认值表示如果在函数调用期间没有传递参数值,函数参数将采用该值。默认值是使用表单的赋值(=)运算符赋值的 关键词名称 =价值。 让我们通过一个函数来理解这一点 大学生 .功能 大学生 包含3个参数,其中2个参数指定了默认值。那么,函数 大学生 接受一个必需的参数( 名字 ),其余两个参数是可选的。
Python3
def student(firstname, lastname = 'Mark' , standard = 'Fifth' ): print (firstname, lastname, 'studies in' , standard, 'Standard' ) |
调用函数时,我们需要记住以下几点:
- 在传递关键字参数的情况下,参数的顺序很重要。
- 一个参数只能有一个值。
- 传递的关键字名称应与实际关键字名称匹配。
- 在调用包含非关键字参数的函数时,顺序很重要。
示例#1: 调用没有关键字参数的函数
Python3
def student(firstname, lastname = 'Mark' , standard = 'Fifth' ): print (firstname, lastname, 'studies in' , standard, 'Standard' ) # 1 positional argument student( 'John' ) # 3 positional arguments student( 'John' , 'Gates' , 'Seventh' ) # 2 positional arguments student( 'John' , 'Gates' ) student( 'John' , 'Seventh' ) |
输出:
John Mark studies in Fifth StandardJohn Gates studies in Seventh StandardJohn Gates studies in Fifth StandardJohn Seventh studies in Fifth Standard
在第一个调用中,只有一个必需参数,其余参数使用默认值。在第二次通话中, 姓氏 标准参数值从默认值替换为新的传递值。从函数的第二次、第三次和第四次调用可以看出,参数的顺序很重要。 示例#2:使用关键字参数调用函数
Python3
def student(firstname, lastname = 'Mark' , standard = 'Fifth' ): print (firstname, lastname, 'studies in' , standard, 'Standard' ) # 1 keyword argument student(firstname = 'John' ) # 2 keyword arguments student(firstname = 'John' , standard = 'Seventh' ) # 2 keyword arguments student(lastname = 'Gates' , firstname = 'John' ) |
输出:
John Mark studies in Fifth StandardJohn Mark studies in Seventh StandardJohn Gates studies in Fifth Standard
在第一个调用中,只有一个必需的关键字参数。在第二个调用中,一个是必需的参数,另一个是可选的(标准),其值将从默认值替换为新的传递值。在第三个调用中,我们可以看到关键字参数的顺序并不重要。 示例#3:一些无效的函数调用
Python3
def student(firstname, lastname = 'Mark' , standard = 'Fifth' ): print (firstname, lastname, 'studies in' , standard, 'Standard' ) # required argument missing student() # non keyword argument after a keyword argument student(firstname = 'John' , 'Seventh' ) # unknown keyword argument student(subject = 'Maths' ) |
上述代码将引发错误,因为:
- 在第一次调用中,没有为参数传递值 名字 这是必需的参数。
- 在第二个调用中,关键字参数后面有一个非关键字参数。
- 在第三个调用中,传递的关键字参数与实际的关键字名称参数不匹配。
在python中使用可变对象作为默认参数值
这必须非常小心。原因是当控件到达函数时,参数的默认值只计算一次
第一次定义。之后,在后续的函数调用中引用相同的值(或可变对象)。 通过这个例子,事情会变得更加清楚
Python3
# mutable default argument values example using python list # itemName is the name of the item that we want to add to list # that is being passed, or if it is not passed then appending in # the default list def appendItem(itemName, itemList = []): itemList.append(itemName) return itemList print (appendItem( 'notebook' )) print (appendItem( 'pencil' )) print (appendItem( 'eraser' )) |
['notebook']['notebook', 'pencil']['notebook', 'pencil', 'eraser']
如果假设在每个函数调用中都创建了一个新的列表,而我们没有将列表传递给它,那么您所期望的是什么
[“笔记本”]
【铅笔】
[“橡皮擦”]
但正如您在每次调用函数时程序的实际输出中所看到的,使用的是相同的列表,新的调用不会产生新的列表。
使用字典的示例
Python3
# mutable default argument values example using python dictionary # itemName is the name of item and quantity is the number of such # items are there def addItemToDictionary(itemName, quantity, itemList = {}): itemList[itemName] = quantity return itemList print (addItemToDictionary( 'notebook' , 4 )) print (addItemToDictionary( 'pencil' , 1 )) print (addItemToDictionary( 'eraser' , 1 )) |
{'notebook': 4}{'notebook': 4, 'pencil': 1}{'notebook': 4, 'pencil': 1, 'eraser': 1}
如果假设在每个函数调用中都创建了一个新字典,那么您的预期是什么
{‘notebook’:4}
{‘pencil’:1}
{‘橡皮擦’:1}
但您可以清楚地看到,程序的实际输出是不同的,它表明在每次后续调用中使用相同的字典。
这里的关键是我们应该避免这种情况。
最佳实践
将默认值指定为none,然后检查函数中预期的list或dictionary参数是否为none。
如果没有,则根据您的要求为其分配一个列表或字典。
Python3
# using None as values of the default arguments print ( '#list' ) def appendItem(itemName, itemList = None ): if itemList = = None : itemList = [] itemList.append(itemName) return itemList print (appendItem( 'notebook' )) print (appendItem( 'pencil' )) print (appendItem( 'eraser' )) # using None as value of default parameter print ( '#dictionary' ) def addItemToDictionary(itemName, quantity, itemList = None ): if itemList = = None : itemList = {} itemList[itemName] = quantity return itemList print (addItemToDictionary( 'notebook' , 4 )) print (addItemToDictionary( 'pencil' , 1 )) print (addItemToDictionary( 'eraser' , 1 )) |
#list['notebook']['pencil']['eraser']#dictionary{'notebook': 4}{'pencil': 1}{'eraser': 1}
在这里,您可以清楚地看到,每次调用函数时,如果列表或字典没有作为参数传递给函数,那么它就会创建一个新的列表或字典。