推荐学习书目
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
dbow
V2EX  ›  Python

这样写应该够有 python 风格了,各位看看。

  •  
  •   dbow ·
    maliubiao · Jun 5, 2014 · 4105 views
    This topic created in 4386 days ago, the information mentioned may be changed or developed.
    def t_join(path, *args):
    ----return "%s/%s" % (path.rstrip("/"), "/".join([p.strip("/") for p in args if p]))

    t_join("/usr", "local", "share/resource")

    =>/usr/local/share/resource
    7 replies    2014-06-10 11:41:34 +08:00
    liluo
        1
    liluo  
       Jun 5, 2014
    import os
    os.path.join("/usr", "local", "share/resource")
    dbow
        2
    dbow  
    OP
       Jun 5, 2014
    @liluo 就是重写的os.path.join
    jarlyyn
        3
    jarlyyn  
       Jun 5, 2014
    感觉这是js风格,不是python风格
    keakon
        4
    keakon  
       Jun 6, 2014
    @dbow 逻辑错了

    原版,自己脑补空格吧:
    def join(a, *p):
    """Join two or more pathname components, inserting '/' as needed.
    If any component is an absolute path, all previous path components
    will be discarded. An empty last part will result in a path that
    ends with a separator."""
    path = a
    for b in p:
    if b.startswith('/'):
    path = b
    elif path == '' or path.endswith('/'):
    path += b
    else:
    path += '/' + b
    return path
    mengzhuo
        5
    mengzhuo  
       Jun 6, 2014
    t_join("/usr", "local", "share/resource")

    *args 属于implicit,不符合Pythonic的Explicit
    罚LZ
    import this
    yueyoum
        6
    yueyoum  
       Jun 6, 2014
    @mengzhuo

    *args **kwargs 用处很大, 给代码带来很多自由和方便
    而且 LS 也贴出了原版代码, 都用了 *args
    jprovim
        7
    jprovim  
       Jun 10, 2014
    @keakon 我感覺原版的更容易理解.
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1051 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 40ms · UTC 19:02 · PVG 03:02 · LAX 12:02 · JFK 15:02
    ♥ Do have faith in what you're doing.