位置参数与关键字参数
要把参数传进function中有两种方法,位置参数与关键字参数,示例如下 (如果同时出现,则以位置参数优先)
可以在定义函数时,设定默认值,若使用function时沒有填入改参数,則使用默认值,有的话则覆盖
defmenu(wine, entree, dessert):return {'wine': wine, 'entree': entree, 'dessert': dessert}print(menu('chardonnay', 'chicken', 'cake'))print(menu(entree='beef', dessert='bagel', wine='bordeaux'))print(menu('frontenac', dessert='flan', entree='fish'))
{'wine': 'chardonnay', 'entree': 'chicken', 'dessert': 'cake'}
{'wine': 'bordeaux', 'entree': 'beef', 'dessert': 'bagel'}
{'wine': 'frontenac', 'entree': 'fish', 'dessert': 'flan'}
def menu(wine, entree, dessert='pudding'):return {'wine': wine, 'entree': entree, 'dessert': dessert}print(menu('chardonnay', 'chicken'))print(menu('dunkelfelder', 'duck', 'doughnut'))
{'wine': 'chardonnay', 'entree': 'chicken', 'dessert': 'pudding'}
{'wine': 'dunkelfelder', 'entree': 'duck', 'dessert': 'doughnut'}
#特別注意!!!!若把空list当做默认值会发生意料之外的事情
def buggy(arg, result=[]):
result.append(arg)print(result)#第一次使用时OK
buggy('a')#第二次使用时就会残存上次的结果
buggy('b')#可以使用以下方法修改function
defworks(arg):
result=[]
result.append(arg)returnresultdef nonbuggy(arg, result=None):if result isNone:
result=[]
result.append(arg)print(result)
works('a')
works('b')
nonbuggy('a')
nonbuggy('b')
['a']
['a', 'b']
['a']
['b']
使用 * 与 ** 收集位置参数与关键字参数
*收集而成的参数会以Tuples储存
**收集到的会以Dictionary储存
print('全部都给收集器')def print_args(*args):print('Positional argument tuple:', args)
print_args()
print_args(1,2,3)print('\n混合位置参数使用,剩下的都给收集器')def print_more(required1, required2, *args):print('Need this one:', required1)print('Need this one too:', required2)print('All the rest:', args)
print_more('cap', 'gloves', 'scarf', 'monocle', 'mustache wax')
全部都给收集器
Positional argument tuple: ()
Positional argument tuple: (1, 2, 3)
混合位置参数使用,剩下的都给收集器
Need this one: cap
Need this one too: gloves
All the rest: ('scarf', 'monocle', 'mustache wax')
def print_kwargs(**kwargs):print('Keyword arguments:', kwargs)
print_kwargs(wine='merlot', entree='mutton', dessert='macaroon')
Keyword arguments: {'wine': 'merlot', 'entree': 'mutton', 'dessert': 'macaroon'}
function 说明文字
为了提高代码的可读性,可以对自行定义出的函数加上说明文字,他人在使用时就可以使用help叫出文字
defecho(anything):'echo returns its input argument'
returnanythingdefprint_if_true(thing, check):'''Prints the first argument if a second argument is true.
The operation is:
1. Check whether the *second* argument is true.
2. If it is, print the *first* argument.'''
ifcheck:print(thing)
help(echo)print('--------------------------------')
help(print_if_true)print('\n仅叫出文字↓')print(echo.__doc__)print('--------------------------------')print(print_if_true.__doc__)
Help on function echo in module __main__:
echo(anything)
echo returns its input argument
--------------------------------
Help on function print_if_true in module __main__:
print_if_true(thing, check)
Prints the first argument if a second argument is true.
The operation is:
1. Check whether the *second* argument is true.
2. If it is, print the *first* argument.
仅叫出文字↓
echo returns its input argument
--------------------------------
Prints the first argument if a second argument is true.
The operation is:
1. Check whether the *second* argument is true.
2. If it is, print the *first* argument.