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
tioover
V2EX  ›  Python

关于return的细节问题

  •  
  •   tioover ·
    tioover · 2011-09-07 20:04:20 +08:00 · 5165 次点击
    这是一个创建于 4625 天前的主题,其中的信息可能已经有所发展或是发生改变。
    比较常遇到,挺无关紧要的但是觉得有统一的方法比较好
    if xxx:
    result = True
    else:
    result = False
    return result
    这样好,还是
    if xxx:
    return True
    else:
    return False
    这样?
    -----------------------------------
    if xxx:
    //code
    else:
    return False
    这样好,还是
    if not xxx:
    return False
    //code
    这样(可以减少一层缩进)?
    12 条回复    1970-01-01 08:00:00 +08:00
    ssword
        1
    ssword  
       2011-09-07 20:12:12 +08:00
    最后一个问题,更喜欢后者,缩进一多会影响心情的。
    Hyperion
        2
    Hyperion  
       2011-09-07 20:13:45 +08:00
    我比较喜欢直接return, 用if not先行阻断. "在for里用if, 还不如在if else之间用两个for"...

    这属于个人风格问题吧?...
    daqing
        3
    daqing  
       2011-09-07 20:30:24 +08:00
    第一种情况,可以直接return xxx,而不需要if/else,因为那个表达式的结果,就是boolean值。

    第二种情况,我喜欢后者,先return。
    Echoldman
        4
    Echoldman  
       2011-09-07 21:01:44 +08:00
    我对于函数/方法中return的使用这样:对于不同情况下的返回结果,存储在一个result变量里,最后再返回这个结果,也就是当“函数”/“方法”完全执行完毕再用return;因为异常而导致不得不提前结束“函数”/“方法”,直接用return,并返回错误代码(如果可能用一个代码表示)。
    ro00
        5
    ro00  
       2011-09-07 21:03:03 +08:00
    貌似我都随便用的...比较倾向直接return
    dreampuf
        6
    dreampuf  
       2011-09-07 21:11:38 +08:00
    个人风格问题.

    一个入口,最好只有一个出口.
    kaichen
        7
    kaichen  
       2011-09-07 21:18:47 +08:00
    我会看两部分的代码行数而定,如果其中一个很短的话就早点return出去;如果两部分都差不多就完整得写出if-else。
    dndx
        8
    dndx  
       2011-09-07 21:33:15 +08:00
    为了直观,我选择后者。
    前者的风格比较像Pascal,说实话我不是很喜欢。
    reus
        9
    reus  
       2011-09-11 15:40:28 +08:00
    我偏向第一种,因为如果要加代码的话,不需要大修改
    oldman
        10
    oldman  
       2011-09-11 15:42:32 +08:00
    如果是异常流,我倾向于用及早判断,及早返回;如果都是正常流,我倾向于同一出口(不过也别把if else 嵌的太离谱了)
    Livid
        11
    Livid  
    MOD
       2011-09-11 15:46:08 +08:00
    也取决于上下文的嵌套程度。

    引用来自 Pocoo Style Guide 的一句话:

    Try to avoid too deeply nested code by cleverly placing break, continue and return statements.

    http://www.pocoo.org/internal/styleguide/
    lepture
        12
    lepture  
       2011-09-12 01:50:20 +08:00
    if xxx:
    result = True
    else:
    result = False
    return result
    这样好,还是
    if xxx:
    return True
    else:
    return False
    这样?


    if xxx:
    return True
    return False
    这样好,不需要再加一个 else
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3546 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 11:38 · PVG 19:38 · LAX 04:38 · JFK 07:38
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.