为什么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货币(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最终是货币的返回值,因此它也应该是一个至少需要两个参数的函数(self和tax_rate_percentage).
但是,@ coin可以用来装饰许多其他函数,这些函数可能需要不同数量的args和不同的关键字参数,因此像货币这样的装饰器通常采用变量参数来处理所有情况.