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

如何写出python风格的python代码

  •  
  •   kid177 ·
    kid177 · 2013-08-31 12:48:00 +08:00 · 5883 次点击
    这是一个创建于 3895 天前的主题,其中的信息可能已经有所发展或是发生改变。
    这个问题好像有点蛋疼,从c++转到python,总感觉写出来的代码一点都没有python的风格。看上去就想是把c++硬生生给翻译成了python的语言。是因为c++写惯了的缘故吗?还是个人原因,或者有谁能告诉下怎么样可以写出更python风格的python代码T T
    20 条回复    1970-01-01 08:00:00 +08:00
    snip
        1
    snip  
       2013-08-31 12:50:23 +08:00
    我也对这个问题有兴趣,同问
    eatmore
        2
    eatmore  
       2013-08-31 12:51:57 +08:00
    1) 随便找个知名的开源python项目看看
    2) 读那本cookbook
    lisztli
        3
    lisztli  
       2013-08-31 12:59:27 +08:00
    问问题的时候,把"python风格" 改成 "pythonic"
    meta
        5
    meta  
       2013-08-31 13:30:21 +08:00
    我觉得多用python的特性和方言就比较像了,比如slice或者list的遍历操作什么的。
    tioover
        6
    tioover  
       2013-08-31 13:45:32 +08:00   ❤️ 1
    贴一点你认为不够pythonic 的代码呗
    ThunderEX
        7
    ThunderEX  
       2013-08-31 14:39:19 +08:00   ❤️ 1
    @tioover
    if 'head' == word[0:4]: pass # 一看就是C程序员干的!
    if word.startswith('head'):
    pass
    ipconfiger
        8
    ipconfiger  
       2013-08-31 14:53:08 +08:00
    去玩玩一行实现XXX什么的,就可以把各类奇技淫巧都掌握了。但是那个并不是Pythonic,Pythonic就是怎么简单怎么来
    no13bus
        9
    no13bus  
       2013-08-31 16:22:43 +08:00
    zorceta
        10
    zorceta  
       2013-08-31 17:34:17 +08:00   ❤️ 1
    PEP8
    Arion
        11
    Arion  
       2013-08-31 18:41:45 +08:00   ❤️ 1
    如果你只会python 写出来的绝对是python风格。
    pythoner
        12
    pythoner  
       2013-08-31 23:09:08 +08:00
    1,先理解 The Zen of Python
    2,阅读优秀的开源python项目源码
    3,看看pep8,并竟可能的遵循它
    ushuz
        13
    ushuz  
       2013-09-01 00:06:22 +08:00
    import this & pep8
    kid177
        14
    kid177  
    OP
       2013-09-01 08:05:58 +08:00
    @tioover 比如
    for x in xlist:
    for y in ylist:
    if x % 2 == 0 and y % 2 == 0:
    do_some_thing()

    @ThunderEX 类似就是这样的东西。

    @ipconfiger 尽量把代码写的飘逸吗。

    @no13bus 收藏了。

    @eatmore @pythoner 感觉阅读源码确实是比较好的一件事,不过有点吃力。
    liushuaikobe
        15
    liushuaikobe  
       2013-09-01 18:54:07 +08:00
    >>> import this
    The Zen of Python, by Tim Peters

    Beautiful is better than ugly.
    Explicit is better than implicit.
    Simple is better than complex.
    Complex is better than complicated.
    Flat is better than nested.
    Sparse is better than dense.
    Readability counts.
    Special cases aren't special enough to break the rules.
    Although practicality beats purity.
    Errors should never pass silently.
    Unless explicitly silenced.
    In the face of ambiguity, refuse the temptation to guess.
    There should be one-- and preferably only one --obvious way to do it.
    Although that way may not be obvious at first unless you're Dutch.
    Now is better than never.
    Although never is often better than *right* now.
    If the implementation is hard to explain, it's a bad idea.
    If the implementation is easy to explain, it may be a good idea.
    Namespaces are one honking great idea -- let's do more of those!
    >>>
    unionx
        16
    unionx  
       2013-09-01 23:58:55 +08:00
    有人写的Python是Lisp风格,有人写的Python是Java风格

    我还真没见过Python风格的
    metaclass
        17
    metaclass  
       2013-09-02 03:32:30 +08:00
    其实著名的Python项目有些也是用别的风格写的

    比如Django,会有这样的写法:

    context = {
    'protocol': use_https and 'https' or 'http',
    }

    其中use_https是一个bool

    传统Python风格的写法应该是:

    context = {
    'protocol': 'https' if use_https else 'http',
    }

    不过第一种写法由于和a ? b : c差不多,条件放最前面两个赋值放最后面可能会更容易识别一些,用那个写法的还不少
    Ever
        18
    Ever  
       2013-09-02 07:20:57 +08:00
    @metaclass 前者比较传统,后者是到2.5才"新"加的。不过两者在一定情况下并不等同,比如1 and 0 or 2
    raptor
        19
    raptor  
       2013-09-02 08:36:47 +08:00
    没事,只要能跑就行了,看得多写得多以后自然就pythonic了,不要急
    thinkhu
        20
    thinkhu  
       2013-09-02 10:31:23 +08:00
    v2ex是python写的吧,有时候发布主题出错!
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5295 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 08:19 · PVG 16:19 · LAX 01:19 · JFK 04:19
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.