I like to use dictionaries as a form of switchstatment, using booleans settings as keys. Example:
>>> def f(a):
... return {True: -a, a==0: 0, a > 0: a}[True]
...
>>> f(-3)
3
>>> f(3)
3
>>> f(0)
0
The key Trueworks as a case else/ defaultand is returned only if the other key is not evaluated in True. I guess this suggests some sort of evaluation order for iterating the dictionary.
Now check out the following snippet from the latest release from the Python team for the latest versions of branches 2.6, 2.7, 3.1, and 3.2
Hash randomization means that the iterations of dicts and set are unpredictable and different in all Python scripts. Python is never a guaranteed key iteration order in dict or set, and it is recommended that applications never rely on it. Historically, the dict iteration order has not changed very often across releases and always remained consistent between consecutive Python executions. Thus, some existing applications may rely on dict or set ordering.
Does this mean that using dicts as switch calls is no longer possible? Or maybe I should use any other class (e.g., OrderedDictor something else)? Or maybe I'm completely off, and this should not affect it at all?
source