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

python的代码求助?

  •  
  •   shinerene ·
    Xiechen · May 3, 2013 · 3715 views
    This topic created in 4783 days ago, the information mentioned may be changed or developed.
    def query_db(query,args = (),one=False):
    """QUeries the database and returns a list of dictionaries."""
    cur = get_db().execute(query,args)
    rv = cur.fetchall()
    return (rv[0] if rv else None) if one else rv


    最后一句return 的应该怎么理解?
    6 replies    1970-01-01 08:00:00 +08:00
    liluo
        1
    liluo  
       May 3, 2013
    if one:
    if rv:
    return rv[0]
    else:
    return None
    else:
    return rv
    liluo
        2
    liluo  
       May 3, 2013
    if one:
    ....if rv:
    ........return rv[0]
    ....else:
    ........return None
    else:
    ....return rv

    代码缩进不能,请将 . 脑补成空格.
    dreampuf
        3
    dreampuf  
       May 3, 2013
    三元操作符的组合判断。

    Sqlite API, Django ORM 或者 SQLAlchemy 都支持获取一个的操作,建议使用原生API
    e.g. `cur.fetchone()`
    C0VN
        4
    C0VN  
       May 3, 2013
    中间的那一行是这个吧:
    rv = [dict((cur.description[idx][0].lower(), value) for idx, value in enumerate(row)) for row in cur.fetchall()]

    lz看明白这一行没?!


    (True: do...) if xxx else (False: do...)
    hhrmatata
        5
    hhrmatata  
       May 3, 2013
    没有代码缩进好难看啊
    不知道v2ex为什么不支持。
    shinerene
        6
    shinerene  
    OP
       May 3, 2013
    @liluo
    @dreampuf
    @xavierskip 嗯嗯 看了中间一行的解释和最后一行的解释 看懂了!
    谢谢啦!
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1060 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 18:04 · PVG 02:04 · LAX 11:04 · JFK 14:04
    ♥ Do have faith in what you're doing.