8 Nov
2021
8 Nov
'21
7:18 p.m.
On 11/8/21 4:45 AM, Victor Stinner wrote:
Is it implement "like" ascii(obj).encode("ascii") but with minor changes? What changes?
It works like `str()`, but you get ascii-encoded bytes (or an exception if that's not possible). The difference with the built-in ascii is the absence of extra quotes and the `b` indicator when a string is used: ```
u_var = u'abc' b_var = b'abc'
str(u_var) 'abc'
str(b_var) "b'abc'"
ascii(b_var) "b'abc'"
b'%a' % (u_var) # the docs will be updated to refer to %a as "ascii-repr" b"'abc'" # as it mirrors %r but only returns ascii-encoded bytes
bytes.ascii(u_var) b'abc'
-- ~Ethan~