装饰合同 [英] Contract of a decorator

查看:64
本文介绍了装饰合同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

合同:一个以函数为参数并返回一个函数的函数[即,已传递函数的修改(或相同)版本].传递的函数,例如,这里是square.

Contract: A function that takes function as argument and returns a function[i.e., modified(or same) version of passed function]. Passed function, here is, square, for example.

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

装饰器是否只想返回传递函数的装饰版本,而没有其他内容?

Is decorator suppose to return only decorated version of passed function, but nothing else?

推荐答案

它应该只返回一个函数,但是没有什么可以阻止您返回想要的任何东西.

It is supposed to only return a function, but nothing stops you from returning anything you'd want.

>>> 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
>>>

这篇关于装饰合同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆