惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
Docker
博客园 - 三生石上(FineUI控件)
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园_首页
Microsoft Azure Blog
Microsoft Azure Blog
GbyAI
GbyAI
腾讯CDC
酷 壳 – CoolShell
酷 壳 – CoolShell
M
MIT News - Artificial intelligence
Stack Overflow Blog
Stack Overflow Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Jina AI
Jina AI
爱范儿
爱范儿
博客园 - 【当耐特】
雷峰网
雷峰网
S
SegmentFault 最新的问题
美团技术团队
Blog — PlanetScale
Blog — PlanetScale
The GitHub Blog
The GitHub Blog
有赞技术团队
有赞技术团队
G
Google Developers Blog
大猫的无限游戏
大猫的无限游戏
Google DeepMind News
Google DeepMind News
J
Java Code Geeks

博客园 - kylinfish

sentry 9.1.1docker版onepremise过程记录 centos7.2自带的php5.4升级为5.6 ubuntu14.04 编译安装gcc-5.3.0 ubuntu14.04编译安装Git2.7 ubuntu启动器和dash里应用图标不正常 ubuntu14.04使用IceGridAdmin图形界面 在线手册收藏 VIM配置相关记录 virtualbox 错误解决记录 Docker资源收录 mysql workbench的PK,NN,UQ,BIN,UN,ZF,AI GIT非常见命令使用笔记 Google的Python代码格式化工具YAPF详解 在ubuntu上使用QQ的经历 Ubuntu 14.04 下安装Skype pip install lxml mysql-python error 情人节的宠物-测试小工具 API接口数据自检 环信REST API python SDK
python内置函数all使用的坑
kylinfish · 2017-01-16 · via 博客园 - kylinfish

  在代码的改造过程中,因为忽略了一个问题导致数据异常,在改造的过程中以及后续的review中都没注意到这个问题,单元测试也没有覆盖到,记录如下。这个坑在于all的使用上,如果参数为空元组或空列表时,返回值为True,这是要特别注意的地方。改造时忽略了这个地方,应该这样写就对了:

  if not codexes or codex in codexes

原代码:

result = set()
for student_id, codex in rs:
    conditions = []
    if codexes:
        conditions.extend([codex in codexes])
    if all(conditions):
        result.add(student_id)
return result.intersection(set(student_ids))

 新代码改为如下引出问题:

result = set()
for student_id, codex in rs:
    if (codexes and codex in codexes):
        result.add(student_id)
return result.intersection(set(student_ids))

 修补后正确应该为:

result = set()
for student_id, codex in rs:
    if not codexes or codex in codexes:
        result.add(student_id)
return result.intersection(set(student_ids))