zoukankan      html  css  js  c++  java
  • Python 函数的 return 是否是必须的?

    —— Python 函数的 return 是否是必须的?

    —— return [表达式] 语句用于退出函数,选择性地向调用方返回一个表达式。不带参数值的return语句返回None。

    来看一段关于 return 的描述:

    return may only occur syntactically nested in a function definition, not within a nested class definition.

    If an expression list is present, it is evaluated, else None is substituted.

    return leaves the current function call with the expression list (or None) as return value.

    When return passes control out of a try statement with a finally clause, that finally clause is executed before really leaving the function.

    In a generator function, the return statement indicates that the generator is done and will cause StopIteration to be raised. The returned value (if any) is used as an argument to construct StopIteration and becomes the StopIteration.value attribute.

    【译】

    return 可能只发生在函数定义中,而不是在嵌套类定义中。

    如果出现了表达式列表,那么它将被评估,否则替换为 None 。

    return  将当前的函数调用与表达式列表(或None)作为返回值。

    当return通过最后的子句从try语句中获得控制权时,最后的子句在真正离开函数之前执行。

    在生成器函数中,return 语句表明生成器已经完成,并将导致停止迭代。返回值(如果有的话)被用作构造stop迭代的参数,并成为停止迭代的价值属性。

     

    因此,如果不写return语句,那么默认函数结尾会return None

     

  • 相关阅读:
    hadoop 第二次启动 50030端口能打开 50070端口打不开
    hadoop配置(个人总结)
    发布版本化您的应用
    hive 初始化运行流程
    android Content Providers 内容提供者
    hive Cli常用操作(翻译自Hive wiki)
    用于展现图表的50种JavaScript库
    STL list transfer (迁移)实现
    pop_heap算法 (将根节点置于容器尾部后重调heap结构)
    sgi STL源码下载
  • 原文地址:https://www.cnblogs.com/shenxiaolin/p/7944174.html
Copyright © 2011-2022 走看看