V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
IanPeverell
V2EX  ›  Python

Python 的无返回值函数末尾写与不写 return 对代码有无影响

  •  
  •   IanPeverell ·
    ianhhhhhhhhe · 2017-02-03 12:40:22 +08:00 · 10842 次点击
    这是一个创建于 2641 天前的主题,其中的信息可能已经有所发展或是发生改变。

    受之前一篇文章的启发,想到了一个问题,文章中写道

    def re(x):
        print(x)
    
    print(re('hello world'))
    

    执行之后的得到

    hello world
    

    但是实际情况是后面还会跟着一个

    None
    

    然后就突然想到之前在返回 None 的时候有的时候会简写成

    def fun(x):
        something
        if some_reason:
            return
        else:
            return something
    

    所以说在无返回值的时候,这个 return 是可有可无的,还是会对代码造成一定的影响?

    第 1 条附言  ·  2017-02-03 15:26:00 +08:00

    下午闲暇时间去翻了翻文档,发现在关于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语句,那么默认函数结尾会return None

    顺带发现了新东西,exciting!

    15 条回复    2017-02-04 08:39:05 +08:00
    kindjeff
        1
    kindjeff  
       2017-02-03 12:51:34 +08:00 via iPhone
    没有区别
    jimzhong
        2
    jimzhong  
       2017-02-03 12:54:30 +08:00
    return 后没有东西会返回 None
    a87150
        3
    a87150  
       2017-02-03 13:02:26 +08:00
    你这是真 函数式编程
    rocksolid
        4
    rocksolid  
       2017-02-03 13:13:39 +08:00
    默认返回 None
    既然没返回值了,直接用函数去好了
    SuperMild
        5
    SuperMild  
       2017-02-03 13:30:04 +08:00
    这个 re(x) 函数不是得到 x, 而是打印 x, 得到 none. 打印 x 是副作用。

    真正需要考虑的是这个函数要返回什么,如果要返回的刚好就是 none, 就可以省掉 return 了,如果要返回别的东西,比如不打印 x 而是返回 x, 自然就需要写 return 。
    zhuangzhuang1988
        6
    zhuangzhuang1988  
       2017-02-03 13:33:25 +08:00
    ryd994
        7
    ryd994  
       2017-02-03 13:46:09 +08:00
    如果是明确想要返回 None ,还是主动返回比较好
    代码更清晰容易维护,也防自己忘了的坑
    性能不需要担心
    IanPeverell
        8
    IanPeverell  
    OP
       2017-02-03 14:06:55 +08:00
    所以说,在无返回值函数的最末尾, return None , return 以及不写最后的 return 对函数整体没有影响,写不写只会影响最后维护,写了比较清晰,不写也不会导致代码性能不佳的问题
    xuboying
        9
    xuboying  
       2017-02-03 14:20:01 +08:00 via Android
    有些语言可以检查规范要求必须有显示 return 。 python 却没有,很容易出问题。
    likuku
        10
    likuku  
       2017-02-03 15:05:07 +08:00
    看你个人喜好了
    wayslog
        11
    wayslog  
       2017-02-03 15:14:05 +08:00
    None 为 Python 里的 uint type ,所以,默认无返回的函数返回 None
    ansheng
        12
    ansheng  
       2017-02-03 17:11:08 +08:00
    函数没有返回值的时候默认就会返回 None ,所以不奇怪,而且你这个 print(re('hello world')),输出的是 re 的返回值,
    appstore001
        13
    appstore001  
       2017-02-03 21:18:14 +08:00 via Android
    是啊,你就是返回 re 这个函数的返回值。
    如果你不需要返回值,那你 print 也可以去掉。
    BingoXuan
        14
    BingoXuan  
       2017-02-03 23:29:28 +08:00 via iPhone
    我是有次忘了写返回就发现 python 是默认返回 none 的,所以一看到 print 出 None 就知道自己忘写返回值了。
    后来写 c 就养成默认要 return 0
    XYxe
        15
    XYxe  
       2017-02-04 08:39:05 +08:00   ❤️ 1
    In [1]: import dis

    In [2]: def f1():
    ...: pass
    ...:

    In [3]: def f2():
    ...: return
    ...:

    In [4]: def f3():
    ...: return None
    ...:

    In [5]: dis.dis(f1)
    2 0 LOAD_CONST 0 (None)
    3 RETURN_VALUE

    In [6]: dis.dis(f2)
    2 0 LOAD_CONST 0 (None)
    3 RETURN_VALUE

    In [7]: dis.dis(f3)
    2 0 LOAD_CONST 0 (None)
    3 RETURN_VALUE
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   945 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 20:51 · PVG 04:51 · LAX 13:51 · JFK 16:51
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.