[英]Understanding scope and *args and **kwargs in decorator function
为什么wrapper()
函数需要下面的装饰器示例中的*args
和**kwargs
?
def currency(f):
def wrapper(*args, **kwargs):
return '$' + str(f(*args, **kwargs))
return wrapper
class Product(db.Model):
name = db.StringColumn
price = db.FloatColumn
@currency
def price_with_tax(self, tax_rate_percentage):
"""Return the price with *tax_rate_percentage* applied.
*tax_rate_percentage* is the tax rate expressed as a float, like "7.0"
for a 7% tax rate."""
return price * (1 + (tax_rate_percentage * .01))
传递给price_with_tax(self, tax_rate_percentage)
的参数是否已经在def currency(f)
函数的范围内可用,因此可用于wrapper()
函数?
为什么我们不能直接将f()
传递给wrapper()
?
我只是想了解为什么wrapper()
有*args
和**kwargs
以及两者都是如何将参数传递给price_with_tax(self, tax_rate_percentage)
包装器将函数作为参数并返回另一个函数。 为了使返回的函数尽可能有用(即应用于任何函数或方法),它必须接受任意数量的参数。
考虑装饰者的作用。 这个:
@currency
def price_with_tax(self, tax_rate_percentage):
return price * (1 + (tax_rate_percentage * .01))
基本上只是简写:
def price_with_tax(self, tax_rate_percentage):
return price * (1 + (tax_rate_percentage * .01))
price_with_tax = currency(price_with_tax)
也就是说, price_with_tax
最终是currency
的返回值,因此它也应该是一个至少需要两个参数的函数( self
和tax_rate_percentage
)。
但是, @currency
可以用来装饰许多其他函数,这些函数可能需要不同数量的args和不同的关键字参数,因此像currency
这样的装饰器通常采用变量参数来处理所有情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.