教程集 www.jiaochengji.com
教程集 >  Python编程  >  Python入门  >  正文 Python之被装饰函数参数的设置与定义

Python之被装饰函数参数的设置与定义

发布时间:2021-12-16   编辑:jiaochengji.com
教程集为您提供Python之被装饰函数参数的设置与定义等资源,欢迎您收藏本站,我们将为您提供最新的Python之被装饰函数参数的设置与定义资源

被装饰函数参数的设置与定义

先来看一段代码

<pre class="brush:php;toolbar:false">    import time          def timmer(func):         def inner():             start_time=time.time()             func()             end_time=time.time()             print("run time: %s " %(end_time-start_time))         return inner          @timmer     def index():         time.sleep(2)         print("welcome to index page")          @timmer     def home(name):         time.sleep(3)         print("welcome to %s home page" % name)</pre>

如上所示,home函数添加了一个参数,而index函数并没有参数。按照正常的函数的定义与调用方式,调用index函数和home函数的方式应该是下面这种形式:

<pre class="brush:php;toolbar:false">index() home("python")</pre>

然后我们运行程序就会发现,程序抛出了异常。

<pre class="brush:php;toolbar:false">  File "E:\python_learn\py_code\test.py", line 28, in <module>     home("python") TypeError: inner() takes 0 positional arguments but 1 was given</pre>

说个异常说明inner函数不需要位置参数,但是我们给了一个位置参数。回到timmer装饰器定义的部分,可以看到,timmer装饰器的内部函数确实没有定义参数。这样一来,timmer装饰器只能用于装饰没有参数的函数了,我们可以在timmer装饰器定义的时候为inner函数添加一个参数。

<pre class="brush:php;toolbar:false">    import time          def timmer(func):         def inner(name):             start_time=time.time()             func(name)             end_time=time.time()             print("run time: %s " %(end_time-start_time))         return inner          @timmer     def index():         time.sleep(2)         print("welcome to index page")          @timmer     def home(name):         time.sleep(3)         print("welcome to %s home page" % name)          index()     home("python")</pre>

但是这样一来,timmer装饰器装饰index函数的时候又会抛出异常,因为index函数没有参数。

<pre class="brush:php;toolbar:false">File "E:\python_learn\py_code\test.py", line 27, in <module> index() TypeError: inner() missing 1 required positional argument: 'name'</pre>

相关推荐:《Python视频教程》

在不知道被装饰函数的参数个数的情况下,即被装饰函数的参数可变长,且形式不固定的时候,可以使用*args和**kwargs,把上面的代码修改:

<pre class="brush:php;toolbar:false">    import time          def timmer(func):         def inner(*args,**kwargs):             start_time=time.time()             func(*args,**kwargs)             end_time=time.time()             print("run time: %s " %(end_time-start_time))         return inner          @timmer     def index():         time.sleep(2)         print("welcome to index page")          @timmer     def home(name):         time.sleep(3)         print("welcome to %s home page" % name)          index()     home("python")</pre>

再次运行程序,查看运行结果:

<pre class="brush:php;toolbar:false">welcome to index page run time: 2.0  welcome to python home page run time: 3.0</pre>

由上可知,在不知道被装饰函数的参数个数时,可以使用*args和**kwargs来表示任意长度任意形式的参数。

被装饰函数的返回值

修改上面的代码,为home函数定义一个返回值,分别打印index函数和home函数的返回值。

<pre class="brush:php;toolbar:false">    import time          def timmer(func):         def inner(*args,**kwargs):             start_time=time.time()             func(*args,**kwargs)             end_time=time.time()             print("run time: %s " %(end_time-start_time))         return inner          @timmer     def index():         time.sleep(2)         print("welcome to index page")          @timmer     def home(name):         time.sleep(3)         print("welcome to %s home page" % name)         return("home func")          index_res=index()     print(index_res)          home_res=home("python")     print(home_res)</pre>

运行程序,可以看到:

<pre class="brush:php;toolbar:false">welcome to index page run time: 2.0  None welcome to python home page run time: 3.0  None</pre>

可以看到,home函数中定义的返回值并没有被打印出来,显示的值为None。因为这里执行的home函数不是原始定义的home函数,而是wrapper函数的执行结果。因为wrapper函数并没有定义返回值,所以执行被装饰后的home函数并没有打印出返回值。

修改代码,在timmer装饰器中定义并返回被装饰函数执行的返回值。

<pre class="brush:php;toolbar:false">    import time          def timmer(func):         def inner(*args,**kwargs):             start_time=time.time()             res=func(*args,**kwargs)             end_time=time.time()             print("run time: %s " %(end_time-start_time))             return res         return inner          @timmer     def index():         time.sleep(2)         print("welcome to index page")          @timmer     def home(name):         time.sleep(3)         print("welcome to %s home page" % name)         return("home func")          index_res=index()     print(index_res)          home_res=home("python")     print(home_res)</pre>

再次执行函数,查看执行结果:

<pre class="brush:php;toolbar:false">  welcome to index page     run time: 2.0      None          welcome to python home page     run time: 3.0      home func</pre>

可以看来,原始home函数中定义的返回值被打印出来了。

结论:

(1)如果被装饰函数没有定义返回值,timmer装饰器装饰后的返回值为None。

(2)如果被装饰函数定义了返回值,则timmer装饰器装饰后则返回被装饰函数的返回值。

您可能感兴趣的文章:
Python中使用装饰器的三个技巧
python装饰器以什么开头
Python之装饰器简介
Python之被装饰函数参数的设置与定义
Python functools模块完全攻略
Python内置的wraps装饰器有什么用
python 装饰器详解
python装饰器是什么
Python之装饰器如何使用
python中的装饰器的使用实战

[关闭]
~ ~