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

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
G
Google Developers Blog
V
V2EX
美团技术团队
H
Help Net Security
月光博客
月光博客
爱范儿
爱范儿
Engineering at Meta
Engineering at Meta
The Cloudflare Blog
U
Unit 42
大猫的无限游戏
大猫的无限游戏
Recent Announcements
Recent Announcements
A
About on SuperTechFans
博客园 - Franky
The GitHub Blog
The GitHub Blog
N
Netflix TechBlog - Medium
人人都是产品经理
人人都是产品经理
博客园 - 司徒正美
MyScale Blog
MyScale Blog
B
Blog
雷峰网
雷峰网
Y
Y Combinator Blog
云风的 BLOG
云风的 BLOG
T
The Blog of Author Tim Ferriss

博客园 - 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))