classification
Title: Builtin function all() is handling dict() types in a weird way.
Type: behavior Stage: resolved
Components: Versions: Python 3.8, Python 3.7, Python 3.6, Python 3.4, Python 3.5, Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: stnv, xtreak
Priority: normal Keywords:

Created on 2018-11-06 12:58 by stnv, last changed 2018-11-06 13:28 by josh.r. This issue is now closed.

Files
File name Uploaded Description Edit
all-dict-example.py stnv, 2018-11-06 12:58
Messages (2)
msg329354 - (view) Author: Daniel Stoinov (stnv) Date: 2018-11-06 12:58
When a dictionary is passed to all() function, it iterates the keys instead of the values.
msg329356 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2018-11-06 13:16
Dictionary iterates over keys and this is expected behavior. If you need to iterate by values you should use dict().values()

$ python3 -c 'for i in dict(a=1): print(i)'
a
$ python3 -c 'print(all(dict(a=False)))' # Iterate by keys and thus 'a' is True
True
$ python3 -c 'print(all(dict(a=False).values()))' # Iterate by values explicitly
False

# Relevant PEP section :

https://www.python.org/dev/peps/pep-0234/#dictionary-iterators

> Dictionaries implement a tp_iter slot that returns an efficient iterator that iterates over the keys of the dictionary. During such an iteration, the dictionary should not be modified, except that setting the value for an existing key is allowed (deletions or additions are not, nor is the update() method). This means that we can write

> for k in dict: ...

https://stackoverflow.com/questions/3294889/iterating-over-dictionaries-using-for-loops


This is not a bug but an expected behavior unless I am missing something from the script attached
History
Date User Action Args
2018-11-06 13:28:07josh.rsetstatus: open -> closed
resolution: not a bug
stage: resolved
2018-11-06 13:16:59xtreaksetnosy: + xtreak
messages: + msg329356
2018-11-06 12:58:12stnvcreate