Closed stevenbird closed 8 years ago
I think performing a change v by 1
changes v into a number, where the dictionary wants a string key.
I'll have a think about what to do to fix this.
Another thing about the behaviour of dictionaries is that for 'object' keys, to access an entry, the object passed must be the exact same object as used to set the entry.
E.g. will not work (since the lists are not the same memory location).
This might be confusing to beginners, so we should probably have a think about that sort of behaviour as well. One option might be to convert all keys to strings - then it wouldn't matter if a number type were used to access a string-key value (or vice-versa) and you wouldn't need to use the exact objects to access entries with object keys. There might be other side effects to this though, so I'll investigate and see if there would be issues with this or if there are better options available.
Another possible solution is to maintain a mapping from the string back to the object, and to report the object for some methods (e.g. keys in dict _
), but use the string version in others (e.g. get _ in dict _
).
Alternatively we could try to automatically convert all numeric-like things to numbers so that way the string "2"
would be converted to the number 2
when either getting or setting an entry (much like what is currently done with node ids).
This would still cause non-identical objects to not be considered the same key, but would be an easy fix to the change by ...
problem, avoiding the burden of an extra mapping.
Suppose we only do the conversion on strings that can be losslessly converted to integers. (i.e. s == str(int(s)
)
The following code produces the error:
no entry 2 in dict
: