[Python-ideas] More alternate constructors for builtin type

Chris Angelico rosuav at gmail.com
Tue May 7 14:24:20 EDT 2019


On Wed, May 8, 2019 at 4:17 AM Oscar Benjamin
<oscar.j.benjamin at gmail.com> wrote:
> Admittedly the non-ASCII unicode digit example is not one that has
> actually caused me a problem but what I have had a problem with is
> floats. Given that a user of my code can pass in a float in place of a
> string the fact that int(1.5) gives 1 can lead to bugs or confusion.
> ...
> There is no function to parse a decimal integer string without also
> accepting floats though.

Explicitly parse in decimal, then.

>>> int(1.5, 10)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: int() can't convert non-string with explicit base

ChrisA


More information about the Python-ideas mailing list