V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
herozem
V2EX  ›  Flask

flask 中的 endpoint 的存在的意义是什么?

  •  
  •   herozem · 2016-09-08 23:17:35 +08:00 · 5045 次点击
    这是一个创建于 2779 天前的主题,其中的信息可能已经有所发展或是发生改变。

    RT 。

    e.g.

    def add_url_rule(self, rule, endpoint=None, view_func=None, **options):
    
            Basically this example::
    
                @app.route('/')
                def index():
                    pass
    
            Is equivalent to the following::
    
                def index():
                    pass
                app.add_url_rule('/', 'index', index)
    

    endpoint 作为 url -> endpoint -> func 中间的一层,并没有找到什么特别大的意义?如果说是为了在 url 上进行弄上一层 "namespace" , 例如 /admin/hello/user/hello 都绑住各自目录下 一个叫做 hello 的函数。那为什么不直接 通过 url -> func 的方式将 /admin/hello 和 admin 下的 hello 函数绑起来, /user/hello 和 user 下的 hello 函数绑起来?

    也许上面的描述不太清楚。但是总的意思就是, endpoint 的意义到底是什么?

    12 条回复    2016-12-21 13:48:44 +08:00
    xcv58
        1
    xcv58  
       2016-09-09 05:48:03 +08:00
    确实不清楚,没看懂什么意思。
    zeayes
        2
    zeayes  
       2016-09-09 08:27:47 +08:00
    dispatch request 需要用 endpoint ,只是在 app.route 中,默认是以当前函数名作为 endpoint 。
    guyskk
        3
    guyskk  
       2016-09-09 09:12:38 +08:00 via Android
    flask.url_for 需要通过 endpoint 得到 url ,可以避免匿名函数的问题。
    zhangmiaoCHN
        4
    zhangmiaoCHN  
       2016-09-09 09:19:12 +08:00
    Flask 中的 endpoint 和 Django 中的 name 是一个概念。如果 add_url_rule 不指定 endpoint ,默认是会用函数名比如 index 作为 endpoint 。

    url -> endpoint -> func 其实可以理解为两层的 url --> endpoint(func)
    其中, endpoint 和 func 其实是一个东西,相互别名
    WordCount
        5
    WordCount  
       2016-09-09 11:13:15 +08:00
    aec4d
        6
    aec4d  
       2016-09-09 11:51:30 +08:00   ❤️ 1
    一句话就是函数是可变对象 字符串是不可变对象
    你可以看一下 werkzeug 的 routing.py 部分代码 是一个非常比较独立的模块 其中有这样一段
    `endpoint`
    The endpoint for this rule. This can be anything. A reference to a
    function, a string, a number etc. The preferred way is using a string
    because the endpoint is used for URL generation.

    在 werkzeug 中 endpoint 是 rule 的一个属性
    self._rules_by_endpoint.setdefault(rule.endpoint, []).append(rule)
    主要使用 2 个方法 match 和 build 前者从 url 匹配到 endpoint 后者从 endpoint 还原 url
    如果你使用函数当做 endpoint 第一部分不会有问题 第二部分就不一定了 对函数加一个装饰器它就变了 此时你就无法正确匹配到 rule 对象了
    herozem
        7
    herozem  
    OP
       2016-09-09 16:15:23 +08:00
    @xcv58 尴尬

    @zeayes
    @zhangmiaoCHN 所以为什么不直接用 url -> func 的形式?


    @guyskk 能避免匿名函数的问题?能举个例子吗?


    @WordCount stackoverflow 上的我看到过了,但是还是没有充分的理由啊,就是为了 url_for 一个函数就加一层么。。


    @aec4d 所以说到底,就是为了 url_for 这个函数,所以要在 url 和函数之间加一层么
    aec4d
        8
    aec4d  
       2016-09-09 17:03:16 +08:00
    @herozem 至少我是这么理解的 如果直接使用函数当做 endpoint 那么所有构建 URL 的函数都无法使用 还有对于大型应用毫无疑问函数名是很容易重复出现的 这样连匹配都会出问题 然而 flask 的 Blueprint 就是使用 Blueprint 的名字加上函数名作为 endpoint 解决了这个问题
    zhangmiaoCHN
        9
    zhangmiaoCHN  
       2016-09-12 19:07:27 +08:00
    @aec4d 解释的很全面。

    @herozem 这里也有相关讨论: http://stackoverflow.com/a/19262349

    我试想过把 endpoint 这一层拿走,其实对写应用包括 Route 本身的实现并没有什么影响。
    herozem
        10
    herozem  
    OP
       2016-09-12 19:13:48 +08:00
    @zhangmiaoCHN 嗯,看过了 stackoverflow 上的这篇回答。但是我仍然觉得有疑问就是,直接写 `/admin/greeting` 和 `/user/greeting` 有什么区别吗?另外函数重名其实也是可以避免的,例如不要 `from admin import greeting` 而用 `admin.greeting` 代替就可以了。 说到底,我就是在寻找 endpoint 的存在必要性。不过目前看来,也许只是为了写大项目的时候方便点,并没有绝对的存在必要性。
    zhangmiaoCHN
        11
    zhangmiaoCHN  
       2016-09-12 19:23:00 +08:00
    @herozem 的确是,如果追究到底, endpoint 感觉上的确没有绝对意义上的存在必要。
    noobsheldon
        12
    noobsheldon  
       2016-12-21 13:48:44 +08:00
    endpoint 的值是唯一的,同一模块中可以有同名的 view function (视图函数)。对于 url_for 函数的参数,如果使用函数名作为参数,则无法确定其 url ;使用 endpoint 作为参数,则保证了 url_for 返回确定的 url 。

    <code>

    # encoding: utf-8
    from flask import Flask

    app=Flask(__name__)

    @app.route('/',endpoint="good")
    def index():
    return "Good jod"

    @app.route('/<int:id>',endpoint="bad")
    def index(id):
    return "%s"%id

    if __name__ == "__main__":
    app.run()
    </code>
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2868 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 14:32 · PVG 22:32 · LAX 07:32 · JFK 10:32
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.