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
1989922yan
V2EX  ›  Python

[问题] bool(list(data)),我只认为这个表达式是为了判断list(data),列表是否为空。 是否还有其他的意义存在呢?

  •  
  •   1989922yan · 2013-10-21 09:18:14 +08:00 · 3099 次点击
    这是一个创建于 3833 天前的主题,其中的信息可能已经有所发展或是发生改变。
    第 1 条附言  ·  2013-10-21 10:41:16 +08:00
    ————
    看了大家的回复,我再补充信息:

    在webpy 源码:https://github.com/webpy/webpy

    路径:webpy/web/session.py 中:


    class DBStore(Store):
    """Store for saving a session in database
    Needs a table with the following columns:

    session_id CHAR(128) UNIQUE NOT NULL,
    atime DATETIME NOT NULL default current_timestamp,
    data TEXT
    """
    def __init__(self, db, table_name):
    self.db = db
    self.table = table_name

    # --------------- HERE 看着里^ ^
    def __contains__(self, key):
    data = self.db.select(self.table, where="session_id=$key", vars=locals())
    -> return bool(list(data))



    这里的作用是什么呢?
    第 2 条附言  ·  2013-10-21 15:03:46 +08:00
    OK

    谢谢大家!!

    谢谢你们跟我聊啊!!!

    你们要是不跟我聊!!!

    就没有人跟我聊了啊!!!
    8 条回复    1970-01-01 08:00:00 +08:00
    mengzhuo
        1
    mengzhuo  
       2013-10-21 09:28:58 +08:00
    对于if等条件语句没有意义,其他的,比如UT就不好说了

    都是调用__cmp__的鸭子而已
    Keyes
        2
    Keyes  
       2013-10-21 09:34:00 +08:00
    PYTHON的话,其实我通常情况下判断列表是否为空是用if len(data)。。。非零即真
    kingxsp
        3
    kingxsp  
       2013-10-21 09:37:03 +08:00
    判断列表是否为空 if not data:
    9hills
        4
    9hills  
       2013-10-21 11:09:34 +08:00
    就是判断列表是否为空,没啥意义

    此处这么写比较简洁。。你总不能

    if list(data):
    return True
    else:
    return False

    吧。。
    mengzhuo
        5
    mengzhuo  
       2013-10-21 11:13:47 +08:00
    Python内置的__contains__一定得返回布尔值的

    在这里有用
    Ever
        6
    Ever  
       2013-10-21 15:13:42 +08:00
    @Keyes PEP8是明确反对这种写法的

    For sequences, (strings, lists, tuples), use the fact that empty sequences are false.

    Yes: if not seq:
    if seq:

    No: if len(seq)
    if not len(seq)
    Keyes
        7
    Keyes  
       2013-10-21 20:16:33 +08:00
    @Ever 我之前看pep8时候就觉得很反感这个建议,我习惯的用法是这样

    如果条件是布尔型:
    v = True
    if v

    如果条件是整形:
    v = 1000
    if v == 1000

    如果条件是list、dict:
    v1 = [1, 2, 3]
    if len(v1)

    v2 = {1:"1", 2:"2", 3:"3"}
    if len(v2)

    如果条件是None:
    v = None
    if v is None

    很简单,我要的就是代码可读性高,看到if我就知道变量是什么类型的,不知道这样说能否理解

    诶。。我在想要不要单开一贴说说这问题。。。
    Keyes
        8
    Keyes  
       2013-10-21 20:23:00 +08:00
    @Ever 忘记了字串也是和list、dict的方法一样
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5723 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 02:00 · PVG 10:00 · LAX 19:00 · JFK 22:00
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.