【问题标题】:How to annotate a function in python that returning an html file如何在python中注释返回html文件的函数
【发布时间】:2022-11-01 23:14:36
【问题描述】:

这是我的代码我也尝试过不带引号但它给了我('html'未定义)错误

@app.route('/entry')
def entry_page() -> 'html':
    return render_template('entry.html',
                           the_title='Welcome to searchtools on the web!')
app.run()

【问题讨论】:

    标签: python python-3.x


    【解决方案1】:

    你真的可以设置任何东西,但在这些场景中,我发现添加注释作为返回类型很有用。就我而言,PyCharm 似乎也理解我正在添加评论并删除警告,因为它知道这样做确实很好。

    例子:

    def entry_page() -> 'some kind of valid html':
        return ...  # something here
    

    【讨论】:

    • 虽然它有效,但它不是合适的。大多数代码编辑器会显示错误(但该功能会起作用),甚至库也不使用它们。
    【解决方案2】:

    这不是注释的工作方式 -> 是一个运算符,它提到要返回的数据的 type。 注释函数有多种方法。

    方法一:评论 (也称为单行注释)

    def function(a): # Annotation
        return a
    

    方法二:字符串字面量 (也称为多行或文档字符串注释)

    def function(a):
        """
        Returns the value passed
        """
        return a
    

    方法 1 推荐用于小型 cmets(对于你的情况)大cmets推荐方法2

    【讨论】:

      猜你喜欢
      • 2013-12-31
      • 2019-05-30
      • 2021-10-31
      • 2019-02-15
      • 1970-01-01
      • 1970-01-01
      • 2018-12-15
      • 2015-10-04
      • 2016-07-20
      相关资源
      最近更新 更多