[Python-Dev] Generalised String Coercion

"Martin v. Löwis" martin at v.loewis.de
Mon Aug 8 10:07:37 CEST 2005


Phillip J. Eby wrote:
>>Hm. What would be the use case for using %s with binary, non-text data?
> 
> 
> Well, I could see using it to write things like netstrings, 
> i.e.  sock.send("%d:%s," % (len(data),data)) seems like the One Obvious Way 
> to write a netstring in today's Python at least.  But perhaps there's a 
> subtlety I've missed here.

As written, this would stop working when strings become Unicode. It's
pretty clear what '%d' means (format the number in decimal numbers,
using "\N{DIGIT ZERO}" .. "\N{DIGIT NINE}" as the digits). It's not
all that clear what %s means: how do you get a sequence of characters
out of data, when data is a byte string?

Perhaps there could be byte string literals, so that you would write

  sock.send(b"%d:%s," % (len(data),data))

but this would raise different questions:
- what does %d mean for a byte string formatting? str(len(data))
  returns a character string, how do you get a byte string?
  In the specific case of %d, encoding as ASCII would work, though.
- if byte strings are mutable, what about byte string literals?
  I.e. if I do

  x = b"%d:%s,"
  x[1] = b'f'

  and run through the code the second time, will the literal have
  changed? Perhaps these would be displays, not literals (although
  I never understood why Guido calls these displays)

Regards,
Martin


More information about the Python-Dev mailing list