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

以下两种风格 Python 写法,请问大家倾向哪种:)

  •  
  •   lucat · 2018-12-10 22:19:28 +08:00 · 8905 次点击
    这是一个创建于 1967 天前的主题,其中的信息可能已经有所发展或是发生改变。

    看到不同的同事代码,表达同一个意思,主要 if 部分以下哪种写法比较好些:)

    第一种写法

    def xxxx(ph, sin_id):
        """
        
        """
        
        with OracleConnect(xxxx) as db_oracle:
        	
            sql = u"xxxxx"
            has_data, sql_data = db_oracle.get_one(sql)
            
            # 第一种写法
            if has_data:
                calculate_func = "rollback"
                task_id = sql_data.get("task_id", "")
            else:
                calculate_func = "calculate"
                task_id = sin_id
                
    	return task_id, calculate_func
    

    第二种写法

    def xxxx(ph, sin_id):
        """
        
        """
        
        with OracleConnect(xxxx) as db_oracle:
        	
            sql = u"xxxxx"
            has_data, sql_data = db_oracle.get_one(sql)
            
            # 第二种写法
            calculate_func = "rollback" if has_data else "calculate"
            task_id = sql_data.get("task_id", "") if has_data else sin_id   
            
       	return task_id, calculate_func
    

    第一种写法,比较直接,清晰明了,但有同事说不够 pythonic

    第二种写法比较 pythonic,但是类似写法,对于刚接手别人代码的同事,就要稍微绕一下才能读懂。

    由此拓展,想到在写业务逻辑中,经常会有 if 语句的判断,请问大家是怎么选择代码风格的:)

    107 条回复    2018-12-23 18:29:28 +08:00
    1  2  
    menc
        101
    menc  
       2018-12-12 11:39:28 +08:00
    @congeec 都无所谓,hashable type 就可以这么用,list 不行,list 改成 tuple 行,但是数据库返回结果应该是 tuple 不是 list
    menc
        102
    menc  
       2018-12-12 11:40:52 +08:00
    @congeec 都无所谓,hashable type 就可以这么用,list 不行,list 改成 tuple 行,但是数据库返回结果应该是 tuple 不是 list。
    onice
        103
    onice  
       2018-12-12 12:07:36 +08:00
    毫无疑问是第一种。如果代码是写给自己看,可以用第二种。
    mili8908
        104
    mili8908  
       2018-12-13 09:17:21 +08:00
    简单的 if 判断当然第二种 你不觉得很美吗=。=
    JacketPC
        105
    JacketPC  
       2018-12-13 21:31:57 +08:00
    自己写无所谓,但写给别人的,不觉得第一个好方便检查吗?
    Qzier
        106
    Qzier  
       2018-12-14 10:12:10 +08:00
    第一种写法,因为 The Zen of Python 中已经阐明:Explicit is better than implicit。
    iorilu
        107
    iorilu  
       2018-12-23 18:29:28 +08:00
    基于同一个变量判断的, 肯定放一起好

    基于这个例子是 1 好, 如果单独变量可以考虑 2
    1  2  
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2907 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 10:43 · PVG 18:43 · LAX 03:43 · JFK 06:43
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.