sgzwiz / brython

Automatically exported from code.google.com/p/brython
BSD 3-Clause "New" or "Revised" License
0 stars 1 forks source link

Sorting dictionary keys lead to incorrect dictionary access #16

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
With console_fr.html:

x = { "Z" : 26, "A" : 1 }
for i in x.keys():
    print(i, x[i])

n = x.keys()
n.sort()
print(n)
for i in n:
    print(i, x[i])

Reports:

Z 26            <= correct
A 1             <= correct
['A','Z']
A 26            <= wrong
Z 1             <= wrong
<done in 26 ms>

The given program is invalid when testing with an old python3 (3.1.2)
Traceback (most recent call last):
  File "z.py", line 6, in <module>
    n.sort()
AttributeError: 'dict_keys' object has no attribute 'sort'

Possible workaround when using dictionary keys:
n = list( x.keys() )

By the way: same issue with "values()"

Original issue reported on code.google.com by pedro.ro...@gmail.com on 27 Dec 2012 at 3:44

GoogleCodeExporter commented 9 years ago
Thanks for the report, that's very helpful
The bugs should be fixed now (version 20121228-090636) : keys(), values() and 
items() return an iterator object, without a sort() method
I close the issue
- Pierre

Original comment by pierre.q...@gmail.com on 28 Dec 2012 at 8:15