You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Python 3.6 change in dict iteration when inserting keys #73606
The behavior of dict iteration has changed in Python 3.6, in that inserting keys during iteration has a different and unpredictable affect. For this code:
Python 3.5 prints a single 'foo' (one pass through the loop). Python 3.6 generates five lines of 'foo' (five passes through the loop). Of course this code is pathological, but I found this behavior from a bug in code where the pathology was a lot less obvious - see nipy/nipy#420
The normal rule (not just for Python) is that a data structures have undefined behavior for mutating while iterating, unless there is a specific guarantee
"""
The change in Python 3.6 is just a different undefined behavior.
Activity
MatthewBrett commentedon Feb 2, 2017
The behavior of dict iteration has changed in Python 3.6, in that inserting keys during iteration has a different and unpredictable affect. For this code:
Python 3.5 prints a single 'foo' (one pass through the loop). Python 3.6 generates five lines of 'foo' (five passes through the loop). Of course this code is pathological, but I found this behavior from a bug in code where the pathology was a lot less obvious - see nipy/nipy#420
bitdancer commentedon Feb 2, 2017
This "bug" has always existed, its just that its manifestation has changed. See bpo-19332 for background.
MatthewBrett commentedon Feb 2, 2017
To clarify from comments on bpo-19332:
"""
"""
The change in Python 3.6 is just a different undefined behavior.