filbert icon indicating copy to clipboard operation
filbert copied to clipboard

Dicts fail to distinguish between string and numeric keys

Open dzhang314 opened this issue 9 years ago • 2 comments

a = {1: "I'm an int", 1.0: "I'm a float", "1": "I'm a string"}
self.say(a[1])   # => "I'm a string"
self.say(a[1.0]) # => "I'm a string"
self.say(a["1"]) # => "I'm a string"
self.say(len(a)) # => 1

Python dicts treat keys of different types as distinct, so a should be a 3-item dict with keys 1, 1.0, and "1". Currently, Filbert treats it as only containing one item corresponding to all three keys.

dzhang314 avatar May 18 '15 20:05 dzhang314

Can you double check that it's meant to distinguish between floats and ints as keys? Because testing in IDLE shows it will treat them as the same value, whichever key is used first is used as the key but the last usage of it will determine the value as it overwrites it. Alternatively figuring out where in a spec or documentation it says how to treat it would be even better.

Xavion3 avatar Jun 02 '15 09:06 Xavion3

You're right! Thanks, I had never noticed that Python fails to distinguish between floats and ints when used as dict keys; I have no idea where this is documented. Python definitely does distinguish between numeric and string types, though, so that still remains to be fixed:

david@cmtd14:~$ python
Python 2.7.6 (default, Mar 22 2014, 22:59:56) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> d = dict()
>>> d[1.0] = "I'm a number!"
>>> d["1"] = "I'm a string that looks like an int!"
>>> d["1.0"] = "I'm a string that looks like a float!"
>>> d["1.00"] = "I'm another string that looks like the same float!"
>>> d
{'1': "I'm a string that looks like an int!", 1.0: "I'm a number!", '1.0': "I'm a string that looks like a float!", '1.00': "I'm another string that looks like the same float!"}

dzhang314 avatar Jun 02 '15 14:06 dzhang314