python内置函数all使用的坑
2017-01-16 14:31
246 查看
在代码的改造过程中,因为忽略了一个问题导致数据异常,在改造的过程中以及后续的review中都没注意到这个问题,单元测试也没有覆盖到,记录如下。这个坑在于all的使用上,如果参数为空元组或空列表时,返回值为True,这是要特别注意的地方。改造时忽略了这个地方,应该这样写就对了:
if not codexes or codex in codexes
原代码:
新代码改为如下引出问题:
修补后正确应该为:
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))
相关文章推荐
- Python【map、reduce、filter】内置函数使用说明(转载)
- Python标准库:内置函数all(iterable)
- python __call__ 内置函数的使用
- 装饰器、生成器、迭代器、及python中内置函数的使用
- Python使用re.findall返回一个查询的列表
- Python【map、reduce、filter】内置函数使用说明
- Python是使用pip 下载 出现... c++ compile .... vcvarsall.bat 找不到
- python __call__ 内置函数的使用
- Python 使用mingw构建第三方库 Unable to find vcvarsall.bat错误解决方法
- Python标准库:内置函数hasattr() getattr() setattr() 函数使用方法详解
- Python标准库:内置函数all(iterable)
- python 内置函数map、reduce的使用
- python 函数定义和内置函数isinstance以及数据类型检查type的使用
- python __call__ 内置函数的使用
- Python【map、reduce、filter】内置函数使用说明(转载)
- python re.findall()使用2个模型查找返回包含tuple的list
- python使用pip安装gevent时提示Unable to find vcvarsall.bat
- python_8(自定义模型使用、内置函数)
- python学习(2):自定义包结构及__init__.py模块和__all__变量的使用
- Python: Windows下使用pip安装库出错:Microsoft Visual C++ 9.0 is required < Unable to find vcvarsall.bat