[Python-ideas] More alternate constructors for builtin type

Oscar Benjamin oscar.j.benjamin at gmail.com
Tue May 7 15:14:24 EDT 2019


On Tue, 7 May 2019, 19:25 Chris Angelico, <rosuav at gmail.com> wrote:

> 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
>

Good point!

Maybe that could be the solution for bytes as well: overload the
constructor even more and allow the second argument to be an integer base:

    >>> bytes(123, 10)
    b'123'

--
Oscar

>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20190507/9dcf3649/attachment-0001.html>


More information about the Python-ideas mailing list