gpt4 book ai didi

python - 装修契约(Contract)

转载 作者:行者123 更新时间:2023-11-28 20:02:36 24 4
gpt4 key购买 nike

契约:一个函数,它以函数作为参数并返回一个函数[即,修改的(或相同的)传递函数的版本]。传递函数,这里以square为例。

@floatify
def square(n):
return n*n

装饰器是否应该只返回传递函数的装饰版本,而不返回其他任何东西?

最佳答案

它应该只返回一个函数,但没有什么能阻止你返回你想要的任何东西。

>>> def d(x):
... return "hello"
...
>>> @d
... def f():
... return "world"
...
>>> f
'hello'
>>> f()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object is not callable
>>>

关于python - 装修契约(Contract),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45640690/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com