On Fri, Sep 04, 2020 at 06:10:23PM -0400, Cade Brown wrote:
> I mentioned that in my post; however it doesn't satisfy the problems I have
> (mainly being that eval(repr(x))==x)
I'm not very sympathetic to that problem. `eval(repr(x))` is a Nice To
Have convenience for the interactive interpreter, it is not a guarantee,
and it is not best practice when coding -- especially not if your x
values are objects that come from a third party.
`float(repr(x))` works fine and avoids both the security and performance
issues with eval:
py> from math import inf
py> nums = [1.2, -8.9e-50, inf]
py> all([float(repr(x)) == x for x in nums])
True
So I'm going to climb out on a limb here and say that if you're
calling eval on the repr of floats, you're probably doing the wrong
thing. In fact, if you know that they are certainly floats, and never
any other kind of object, then you are *certainly* doing the wrong thing
because of performance:
$ python3 -m timeit "eval('1.2575')"
100000 loops, best of 5: 3.21 usec per loop
$ python3 -m timeit "float('1.2575')"
2000000 loops, best of 5: 113 nsec per loop
And if you *don't* know what sort of objects they are, then you're
leaving yourself open to a world of hurt if there is any possibility you
might get them from an untrusted third-party.
--
Steve
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/6DKETUBLVANL737YW3E7KYI5DT2ST5H3/
Code of Conduct: http://python.org/psf/codeofconduct/