Python 3.5, bytes, and %-interpolation (aka PEP 461)

Marko Rauhamaa marko at pacujo.net
Mon Feb 24 15:46:37 EST 2014


Ethan Furman <ethan at stoneleaf.us>:

> Can anybody think of a use-case for this particular feature?

Internet protocol entities constantly have to format (and parse)
ASCII-esque octet strings:

    headers.append(b'Content-length: %d\r\n' % len(blob))

    headers.append(b'Content-length: {}\r\n'.format(len(blob)))

Now you must do:

    headers.append(('Content-length: %d\r\n' % len(blob)).encode())

    headers.append('Content-length: {}\r\n'.format(len(blob)).encode())

That is:

 1. ineffient (encode/decode shuffle)

 2. unnatural (strings usually have no place in protocols)

 3. confusing (what is stored as bytes, what is stored as strings?)

 4. error-prone (UTF-8 decoding exceptions etc)


To be sure, %s will definitely be needed as well:

   uri = b'http://%s/robots.txt' % host


Marko



More information about the Python-list mailing list