Why Is Dictionary Ordering Non-Deterministic Post-Python 3.3?

Linda Hamilton
Release: 2024-10-21 15:22:02
Original
342 people have browsed it

Why Is Dictionary Ordering Non-Deterministic Post-Python 3.3?

Why is Dictionary Ordering Non-Deterministic in Python 3.3?

In Python versions prior to 3.3, the ordering of dictionary keys was arbitrary but consistent. However, in Python 3.3, this ordering became non-deterministic.

This change results from a security fix implemented in 2012 and enabled by default in Python 3.3. Hash randomization, introduced to address security vulnerabilities, causes the iteration order of dictionaries and sets to be unpredictable and to differ across Python runs.

To disable hash randomization, you can set the PYTHONHASHSEED environment variable to 0.

Counterintuitive Example

While the counterexample in the question does not consistently yield the same result in Python 3.3, its limited number of different orderings stems from the handling of hash collisions.

This limitation is no longer present in Python 3.6 and later versions, where the insertion order of dictionaries is always preserved.

The above is the detailed content of Why Is Dictionary Ordering Non-Deterministic Post-Python 3.3?. For more information, please follow other related articles on the PHP Chinese website!

source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!