[Python-Dev] Changing string constants to byte arrays in Py3k (original) (raw)

"Martin v. Löwis" martin at v.loewis.de
Sun May 6 08:53:13 CEST 2007


That's not a literal, it's a display. The difference is that a literal denotes the same object every time it is executed. A display creates a new object every time it is executed. (another difference is that a display is a constructed thing which may contain runtime-computed components, unlike a literal).

So if bytes are mutable and also have source-level representation, they should be displays, not literals. So is having mutable bytes just a matter of calling them "byte displays" instead of "byte literals" or does that also require changing something in the back end?

It's certainly also an issue of language semantics (i.e. changes to interpreter code). There are a number of options:

  1. don't support creation of byte values through syntax. Instead, create bytes through a constructor function.
  2. if there is syntax support, make it a display: every time you execute a bytes display, create a new value, which can then be mutated.
  3. if you want it to be a literal, make it immutable: change the type, or add a flag so that it is immutable. Then put it into the co_consts array of the code object. The original complaint was that it shouldn't be in co_consts if it is mutable.

In case these three options aren't clear yet, some examples:

  1. def foo(): return bytes([1,2,3])

    print foo() is foo() # False x = foo() x[0] = 5 # supported

  2. def foo(): return b"\x01\x02\x03" print foo() is foo() # False x = foo() x[0] = 5 # supported

  3. def foo(): return b"\x01\x02\x03" print foo() is foo() # True x = foo() x[0] = 5 # TypeError

HTH, Martin



More information about the Python-Dev mailing list