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

长代码(非字符串)如何满足 PEP8?

  •  
  •   flyaway · 2016-01-29 21:16:30 +08:00 · 3330 次点击
    这是一个创建于 2980 天前的主题,其中的信息可能已经有所发展或是发生改变。

    一直很纠结对一些很长的列表表达式该如何改写使其满足 PEP8 的规范……比如如下代码:
    left_correct = sum([1 for ins in instances if self.is_acc(ins.left_label, ins.left_score)])

    直接拦腰砍断么……感觉很丑啊……

    16 条回复    2016-02-01 09:10:08 +08:00
    billlee
        1
    billlee  
       2016-01-29 21:24:12 +08:00
    a_list = [a_long_expression(item)
           for item in another_list
           if another_expression(item)]
    flyaway
        2
    flyaway  
    OP
       2016-01-29 21:37:15 +08:00
    @billlee 这么砍貌似确实会好看一些……
    cxbig
        3
    cxbig  
       2016-01-29 21:38:59 +08:00
    - 拆成多行, assign 给多个变量
    - 用 alias 缩短长 fn/method
    xuboying
        4
    xuboying  
       2016-01-29 23:00:07 +08:00 via Android
    这种代码可读性好差
    ipconfiger
        5
    ipconfiger  
       2016-01-29 23:06:01 +08:00
    变量名起得长很容易就超标了.......
    raquelken
        6
    raquelken  
       2016-01-29 23:15:17 +08:00
    拆吧
    bigger_than_1 = lambda num: num > 1
    amount = len(filter(bigger_than_1, [1, 2, 3]))
    Delbert
        7
    Delbert  
       2016-01-29 23:34:19 +08:00
    @billlee 不应该是
    a_list = [a_long_expression(item) \
           for item in another_list \
           if another_expression(item)]
    么?
    flyaway
        8
    flyaway  
    OP
       2016-01-30 10:41:09 +08:00
    @xuboying 就是想知道如何写出可读性好的,且符合 pep8 规范的代码……
    fy
        9
    fy  
       2016-01-30 12:28:59 +08:00
    抓大放小,这种小细节就不必在意了,核心是可读性
    gkiwi
        10
    gkiwi  
       2016-01-30 14:50:50 +08:00
    觉的楼主的问题应该是,我写了不符合 pep8 规范的代码, pep8 检查器老报异常很烦人怎么办。。。
    这样子在需要的行后面加上 # noqa
    有图为证:
    gkiwi
        11
    gkiwi  
       2016-01-30 14:51:34 +08:00
    呃呃呃,图丢了,楼主自己 search 下 pep8 noqa 就晓得了~~
    gkiwi
        12
    gkiwi  
       2016-01-30 14:52:51 +08:00
    不过话说回来,对于特别长的表达式,还是换行是一种好的策略,毕竟代码是写给别人看的,阅读方便才是首要任务
    leavic
        13
    leavic  
       2016-01-31 09:51:36 +08:00
    you really need pycharm
    zjq426
        14
    zjq426  
       2016-01-31 22:28:46 +08:00
    拆行+1
    pynix
        15
    pynix  
       2016-02-01 04:05:22 +08:00
    ryd994
        16
    ryd994  
       2016-02-01 09:10:08 +08:00 via Android
    @Delbert 括号内会自动续行吧……
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5456 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 08:51 · PVG 16:51 · LAX 01:51 · JFK 04:51
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.