自定义的Redprint 实现的 route 装饰器,为什么没有在外围函数route接受被装饰的函数而在里面的装饰函数 decorator(f) 接收呢?

来源:2-6 实现Redprint

背包独行客

2019-11-07

自定义的Redprint或者Flask的 Blueprint 实现的 route 装饰器,为什么没有在外围函数route接收被装饰的函数而在里面的装饰函数 decorator(f) 接收呢?
如:
一:
1、这是自定义redprint的route装饰器实现思路:

 def route(self, rule, **options):
        def decorator(f):
	    self.mound.append((f, rule, options))
            return f
        return decorator

2、这是blueprint的route装饰器实现思路:

    def route(self, rule, **options):
        """Like :meth:`Flask.route` but for a blueprint.  The endpoint for the
        :func:`url_for` function is prefixed with the name of the blueprint.
        """

        def decorator(f):
            endpoint = options.pop("endpoint", f.__name__)
            self.add_url_rule(rule, endpoint, f, **options)
            return f

        return decorator

二、这是入门进阶课程装饰器基础学习的关于装饰器的实现思路:

def outer(f):
    def decorator(*args,**kw):
        print(time.time())
        f(*args,**kw)
    return decorator

@outer
def f1(x,y,**kw):
    print('this is f1 function')

有疑问的地方是一和二两种实现思路的区别是:对于接收被装饰的函数前者是 def decorator(f) 后者是 outer(f)
这是什么思路或意思呢?
作为小白很是疑惑,谢谢老师指导!

写回答

2回答

7七月

2019-11-08

这是因为入门基础课里的是纯函数,而另外的一个是写在类里面的方法吧?

0
2
7七月
回复
背包独行客
是的,一个是类一个是 纯函数,本质上其实是一样的。
2019-11-12
共2条回复

7七月

2019-11-08

代码格式化一下,然后 详细描述下,这代码真没法看。

0
1
背包独行客
已重新编辑 描述,请赐教 谢谢!
2019-11-08
共1条回复

Python Flask高级编程之RESTFul API前后端分离精讲

RESTFul+权限管理+token令牌+扩展flask=提升编程思维

2037 学习 · 359 问题

查看课程