摘要:在本节中,你将学习 Python 关键字参数,以及如何使用它们使函数调用更加清晰明了。
Python 关键字参数简介
让我们从一个简单的函数开始,该函数根据售价和折扣计算净价:
def get_net_price(price, discount):
return price * (1-discount)
get_net_price()
函数有两个参数price
(价格)和 discount
(折扣)。
以下展示了如何调用 get_net_price()
函数,以根据价格 100
和折扣 10%
计算净价:
def get_net_price(price, discount):
return price * (1 - discount)
net_price = get_net_price(100, 0.1)
print(f'{net_price: .2f}')
输出:
90.00
在这个示例中,我们使用 f 字符串(f-string
)将输出数字格式化为保留两位小数(f'{net_price: .2f}'
)。
在 (get_net_price(100, 0.1)
这个函数调用中,我们是以**位置参数**的方式传递每个参数的。换句话说,我们先传递了 price
参数,然后传递了 discount
参数。
然而get_net_price(100, 0.1)
这个函数调用存在**可读性问题**。因为仅通过查看这个函数调用,你无法知道哪个参数是 price
,哪个参数是 discount
。
除此之外,在调用 get_net_price()
函数时,你需要知道每个参数的位置。
如果你不知道参数的位置,函数将会错误地计算 net_price
。例如:
def get_net_price(price, discount):
return price * (1-discount)
net_price = get_net_price(0.1, 100)
print(f'{net_price: .2f}')
输出:
-9.9
为了提高可读性,Python 引入了关键字参数。
以下是关键字参数的语法:
fn(parameter1=value1, parameter2=value2)
通过使用关键字参数语法,你无需按照函数定义中的顺序指定参数。
因此,你可以像这样交换参数的位置来调用函数:
fn(parameter2=value2, parameter1=value1)
以下展示了如何使用关键字参数语法来调用 get_net_price()
函数:
def get_net_price(price, discount):
return price * (1 - discount)
net_price = get_net_price(
price=100,
discount=0.1
)
print(f'{net_price: .2f}')
或者:
def get_net_price(price, discount):
return price * (1-discount)
net_price = get_net_price(
discount=0.1,
price=100
)
print(f'{net_price: .2f}')
这两种(调用方式)都会返回相同的结果。
在最后一个参数后面可以加一个逗号(尾随逗号):
def get_net_price(price, discount):
return price * (1-discount)
net_price = get_net_price(
discount=0.1,
price=100,
)
print(f'{net_price: .2f}')
当你使用关键字参数时,重要的是它们的名称,而不是它们的位置。
def get_net_price(price, discount):
return price * (1-discount)
net_price = get_net_price(
price=100,
discount=0.1
)
print(net_price)
请注意,你可以通过混合使用位置参数和关键字参数来调用函数。例如:
def get_net_price(price, discount):
return price * (1-discount)
net_price = get_net_price(
100,
discount=0.1
)
print(f'{net_price: .2f}')
关键字参数和默认参数
假设你有如下的 get_net_price()
函数,它根据售价、税率和折扣来计算净价。
def get_net_price(price, tax_rate=0.07, discount=0.05):
discounted_price = price * (1 - discount)
net_price = discounted_price * (1 + tax_rate)
return net_price
在 get_net_price()
函数中tax
和 discount
参数分别具有 7% 和 5% 的默认值。
以下代码调用了 get_net_price()
函数,并为 tax
和 discount
参数使用了默认值:
def get_net_price(price, tax_rate=0.07, discount=0.05):
discounted_price = price * (1 - discount)
net_price = discounted_price * (1 + tax_rate)
return net_price
net_price = get_net_price(100)
print(f'{net_price: .2f}')
输出:
101.65
假设你想要为 tax
参数使用默认值,但不为 discount
使用默认值。下面的函数调用不能正确工作。
def get_net_price(price, tax_rate=0.07, discount=0.05):
discounted_price = price * (1 - discount)
net_price = discounted_price * (1 + tax_rate)
return net_price
net_price = get_net_price(100, 0.06)
print(f'{net_price: .2f}')
输出:
100.70
……因为 Python 会将 100
赋值给 price
,将 0.1
赋值给 tax
,而不是赋值给 discount
。
要解决这个问题,你必须使用关键字参数:
def get_net_price(price, tax_rate=0.07, discount=0.05):
discounted_price = price * (1 - discount)
net_price = discounted_price * (1 + tax_rate)
return net_price
net_price = get_net_price(
price=100,
discount=0.06
)
print(f'{net_price: .2f}')
输出:
100.58
或者你可以混合使用位置参数和关键字参数:
def get_net_price(price, tax_rate=0.07, discount=0.05):
discounted_price = price * (1 - discount)
net_price = discounted_price * (1 + tax_rate)
return net_price
net_price = get_net_price(
100,
discount=0.06
)
print(f'{net_price: .2f}')
输出:
100.58
Python 关键字参数要求
一旦你使用了关键字参数,后续的参数也必须使用关键字参数。
以下代码会导致错误,因为它在关键字参数之后使用了位置参数:
def get_net_price(price, tax_rate=0.07, discount=0.05):
discounted_price = price * (1 - discount)
net_price = discounted_price * (1 + tax_rate)
return net_price
net_price = get_net_price(
100,
tax=0.08,
0.06
)
print(f'{net_price: .2f}')
错误:
SyntaxError: positional argument follows keyword argument
要解决这个问题,你需要像这样为第三个参数使用关键字参数:
def get_net_price(price, tax_rate=0.07, discount=0.05):
discounted_price = price * (1 - discount)
net_price = discounted_price * (1 + tax_rate)
return net_price
net_price = get_net_price(
100,
tax_rate=0.08,
discount=0.06
)
print(f'{net_price: .2f}')
输出:
101.52
总结
使用 Python 的关键字参数可以使你的函数调用更具可读性和直观性,特别是对于接受多个参数的函数。
在第一个关键字参数之后,所有后续参数也必须使用关键字参数。