Mutable bytes type

Donn Cave donn at drizzle.com
Mon Jan 16 23:25:02 EST 2006


Quoth "Klaas" <mike.klaas at gmail.com>:
| A PEP for a mutable bytes type was composed but withdrawn two years
| ago:
...
| Does anyone know if a well-coded and tested implementation of such a
| beast exists now?  Strings make me sad.

I didn't look at the PEPs - PEPs always make me sad - but if you
want to just do it, try something like this

	import array

	class Muto:
		def __init__(self, a):
			self.data = array.array('c', a)
		def __str__(self):
			return self.data.tostring()
		def __repr__(self):
			return repr(self.data.tostring())
		def __getitem__(self, i):
			return self.data[i]
		def __setitem__(self, i, a):
			self.data[i] = a

	m = Muto('spam')
	m[1] = 'h'
	print m, [m]

I probably left out a few methods you would want.

	Donn Cave, donn at drizzle.com



More information about the Python-list mailing list