filbert
filbert copied to clipboard
Dicts fail to distinguish between string and numeric keys
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 dict
s 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.
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.
You're right! Thanks, I had never noticed that Python fails to distinguish between float
s and int
s 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!"}