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

     

  • 相关阅读:
    java工程师面试总结
    多线程面试题
    冒个泡
    给大家简单介绍一下:Elasticsearch
    单点登录
    (jQuery)Cookie记住用户名和密码
    我们需要循序渐进的代码重构
    Java序列化(Serialization)的理解
    Java对象序列化
    【武】做一个有自控力的人,开始你的时间规划吧!
  • 原文地址:https://www.cnblogs.com/shenxiaolin/p/7944174.html
Copyright © 2011-2022 走看看