Storing instances using jsonpickle

Chris Angelico rosuav at gmail.com
Fri Sep 5 07:08:09 CEST 2014


On Fri, Sep 5, 2014 at 2:38 PM, Dan Sommers <dan at tombstonezero.net> wrote:
> On Thu, 04 Sep 2014 15:17:17 +1000, Chris Angelico wrote:
>
>> On Thu, Sep 4, 2014 at 9:39 AM, MRAB <python at mrabarnett.plus.com> wrote:
>
>>> The key of a dict could also be int, float, or tuple.
>>
>> Yes! Yes! DEFINITELY do this!! Ahem. Calm down a little, it's not that
>> outlandish an idea...
>
> Using floats is a bad idea.  Consider this python code:
>
>     dictionary = dict()
>     original = get_some_floating_point_value()
>     dictionary[original] = 'foo'
>     string_version = str(original)  # this is where things head south
>     duplicate = float(string_version)
>     value = dictionary.get(duplicate)
>
> Okay, so what is value?  Is it 'foo'?  Is it None?
>
> (Yes, I can fix this.  If I *know* that original is a float, then I
> could use original.hex() instead of str(original).)

There are issues with direct lookups, yes, but you can safely and
easily iterate over that dictionary, and that's going to have plenty
of use.

ChrisA



More information about the Python-list mailing list