[Python-ideas] Adding str.isascii() ?

Guido van Rossum guido at python.org
Fri Jan 26 11:20:49 EST 2018


On Fri, Jan 26, 2018 at 12:42 AM, INADA Naoki <songofacandy at gmail.com>
wrote:

> Currently, int(), str.isdigit(), str.isalnum(), etc... accepts
> non-ASCII strings.
>
> >>> s =  123"
> >>> s
> '123'
> >>> s.isdigit()
> True
> >>> print(ascii(s))
> '\uff11\uff12\uff13'
> >>> int(s)
> 123
>
> But sometimes, we want to accept only ascii string.  For example,
> ipaddress module uses:
>
> _DECIMAL_DIGITS = frozenset('0123456789')
> ...
> if _DECIMAL_DIGITS.issuperset(str):
>
> ref: https://github.com/python/cpython/blob/e76daebc0c8afa3981a4c5a8b54537
> f756e805de/Lib/ipaddress.py#L491-L494
>
> If str has str.isascii() method, it can be simpler:
>
> `if s.isascii() and s.isdigit():`
>
> I want to add it in Python 3.7 if there are no opposite opinions.
>

That's fine with me. Please also add it to bytes and bytearray objects.
It's okay if the implementation has to scan the string -- so do isdigit()
etc.

-- 
--Guido van Rossum (python.org/~guido)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180126/8fb2f7de/attachment-0001.html>


More information about the Python-ideas mailing list