Chinaunix

标题: all(),any()的矛盾 [打印本页]

作者: mihello    时间: 2012-10-09 10:13
标题: all(),any()的矛盾
本帖最后由 mihello 于 2012-10-09 10:14 编辑
  1. >>> all([])
  2. True
  3. >>> help(all)
  4. Help on built-in function all in module builtins:

  5. all(...)
  6.     all(iterable) -> bool
  7.    
  8.     Return True if bool(x) is True for all values x in the iterable.

  9. >>> any([])
  10. False
  11. >>> help(any)
  12. Help on built-in function any in module builtins:

  13. any(...)
  14.     any(iterable) -> bool
  15.    
  16.     Return True if bool(x) is True for any x in the iterable.
复制代码
空列表的all居然为真,而any为假这个正确。
这为毛了??求解
作者: darkn3ss    时间: 2012-10-09 17:02
http://stackoverflow.com/questio ... for-empty-iterables

以前还没深究过,这个问题挺好
作者: linux_c_py_php    时间: 2012-10-11 22:38
没有原因, 只有手册:
  1. all(iterable)
  2. Return True if all elements of the iterable are true (or if the iterable is empty). Equivalent to:

  3. def all(iterable):
  4.     for element in iterable:
  5.         if not element:
  6.             return False
  7.     return True
  8. any(iterable)
  9. Return True if any element of the iterable is true. If the iterable is empty, return False. Equivalent to:

  10. def any(iterable):
  11.     for element in iterable:
  12.         if element:
  13.             return True
  14.     return False
复制代码





欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2