
python 的装饰器,我查了相关的资料,意思就是一个函数可以传入另一个函数中。
那么我可不可以不是特别规范的理解为这是一种函数中的继承呢?或者是类似继承的形式在函数中的实现呢?谢谢
1 sudoz 2016-02-25 10:04:24 +08:00 显然不是继承,继承是可以重载父类的属性 而装饰器就是字面理解,在方法外面再包一层而无法对被包裹的方法进行改动 |
2 strahe 2016-02-25 10:06:08 +08:00 不能这么理解吧 不是一回事 |
3 SlipStupig 2016-02-25 10:07:40 +08:00 这个理解是不对的!就是在调用某个函数之前先调用装饰器函数, python 函数等于是个毛坯房装饰器等于一个毛坯房多了一盏灯,灯能干嘛不用我说了吧 |
4 ChiangDi   2016-02-25 10:13:07 +08:00 via Android 一个函数传到另一个函数那叫高阶函数啊 |
5 WangYanjie 2016-02-25 10:13:56 +08:00 |
6 jixiangqd 2016-02-25 10:30:11 +08:00 @WangYanjie 很明显不等价好么。。。不要在这误导。。。。 装饰器就是装饰器,可以参考 decorator pattern 的相关介绍。 装饰器和继承也不等价。不要钻牛角尖,非要把它理解多透彻,多用就知道怎么回事了。 |
7 xAx 2016-02-25 10:35:59 +08:00 不用 python 路过,只想说 GOF23 你肯定一点没看 |
8 julyclyde 2016-02-25 10:40:23 +08:00 不是继承,是同名替换,具体行为在装饰器里 |
9 chenxytw 2016-02-25 10:45:17 +08:00 @jixiangqd 然而在 *Python* 就是等价的....是你在误导,自己看官方文档 https://docs.python.org/3/reference/compound_stmts.html#function-definitions https://docs.python.org/3/glossary.html#term-decorator |
10 bramblex 2016-02-25 10:47:18 +08:00 不过就是一个高阶函数在 python 里的语法糖而已 () 把一个函数包装成另一个函数的函数。 |
11 mulog 2016-02-25 10:48:55 +08:00 @jixiangqd 本来就是等价的啊 https://www.python.org/dev/peps/pep-0318/ Current Syntax The current syntax for function decorators as implemented in Python 2.4a2 is: @dec2 @dec1 def func(arg1, arg2, ...): pass This is equivalent to: def func(arg1, arg2, ...): pass func = dec2(dec1(func)) without the intermediate assignment to the variable func . The decorators are near the function declaration. The @ sign makes it clear that something new is going on here. |
12 herozem 2016-02-25 10:49:52 +08:00 装饰器就是语法糖, @bar def foo(): pass 就相当与: foo = bar(foo), 在这里 foo 就不是之前的 foo 了, foo 变成了 bar 里面返回的东西,如果返回函数,那 foo 还是函数,如果返回字符串,那 foo 就是字符串: >>> def bar(func): ... return "hi" ... >>> @bar ... def foo(): ... pass ... >>> foo 'hi' https://github.com/jiajunhuang/blog/blob/master/python_descriptor_and_decorator.rst 我自己写的总结,不知道对你有没有帮助 |
13 jixiangqd 2016-02-25 11:12:36 +08:00 |
14 limbo0 2016-02-25 11:37:58 +08:00 需要了解两个概念 闭包和函数引用 |
16 wellsc 2016-02-25 12:35:02 +08:00 花式闭包 |
17 hahastudio 2016-02-25 12:42:28 +08:00 s-m-n theorem |
18 limbo0 2016-02-25 12:44:24 +08:00 |
19 lxy 2016-02-25 12:58:33 +08:00 其实就是用函数来封装函数。用惯之后特别爽,写个循环都能用上装饰器~ |
20 shyling 2016-02-25 13:04:07 +08:00 装饰器就是高阶函数的一个语法糖嘛=.= |
21 Allianzcortex 2016-02-25 13:04:10 +08:00 |
22 shyling 2016-02-25 13:08:15 +08:00 对了...decorator 不是闭包.你能在 decorator 装饰的函数里访问 decorator 里的局部变量? |
23 youkochan 2016-02-25 13:18:19 +08:00 应该叫复合函数? |
24 MrEggNoodle 2016-02-25 13:20:48 +08:00 找一本叫作《 python 学习手册》的书,我的答案就在里面找到的。我想你的答案也在里面。 |
29 incompatible 2016-02-25 14:09:47 +08:00 via iPhone |
30 feather12315 2016-02-25 15:40:29 +08:00 via Android 同意 17 楼答案,花式闭包。 前几天刚好写过相关 [闭包与装饰器]( https://vvl.me/style/2016/02/17/python%E4%B9%8B%E9%97%AD%E5%8C%85%E4%B8%8E%E8%A3%85%E9%A5%B0%E5%99%A8.html) 装饰器是用来装饰函数的;它返回的是一个函数对象;被装饰函数的标识符指向返回函数的对象;语法糖 @deco 。 若是还不明白的话,参考资料里面有慕课网的超链接。 |
31 startover 2016-02-25 21:53:40 +08:00 如果一个函数被调用时需要增加一些功能,要么重写这个函数,要么利用高阶函数传入这个函数然后在高阶函数内部改造最后返回一个新的函数。后一种方法,即装饰器。 归根结底,装饰器就是利用了高阶函数和闭包的特性实现的语法糖,个人认为不用执着于概念,知道其应用场景就好了。 可以参考我之前写的一篇文章: http://startover.github.io/articles/2015/04/27/python-decorator-mistake/ |
32 wdg8106 2016-03-02 14:39:54 +08:00 |