[Python-3000] Immutable bytes type and dbm modules
Guido van Rossum
guido at python.org
Tue Aug 7 16:48:46 CEST 2007
On 8/6/07, Jeffrey Yasskin <jyasskin at gmail.com> wrote:
> On 8/6/07, Guido van Rossum <guido at python.org> wrote:
> > I'm waiting for a show-stopper issue that can't be solved without
> > having an immutable bytes type.
> Apologies if this has been answered before, but why are you waiting
> for a show-stopper that requires an immutable bytes type rather than
> one that requires a mutable one? This being software, there isn't
> likely to be a real show-stopper (especially if you're willing to copy
> the whole object), just things that are unnecessarily annoying or
> confusing. Hashing seems to be one of those.
Well one reason of course is that we currently have a mutable bytes
object and that it works well in most situations.
> Taking TOOWTDI as a guideline: If you have immutable bytes and need a
> mutable object, just use list().
That would not work with low-level I/O (sometimes readinto() is
useful), and in general list(b) (where b is a bytes object) takes up
an order of magnitude more memory than b.
> If you have mutable bytes and need an
> immutable object, you could 1) convert it to an int (probably
> big-endian), 2) convert it to a latin-1 unicode object (containing
> garbage, of course), 3) figure out an encoding in which to assume the
> bytes represent text and create a unicode string from that, or 4) use
> the deprecated str8 type. Why isn't this a clear win for immutable
IMO there are some use cases where mutable bytes are the only
realistic solution. These mostly have to do with doing large amounts
of I/O reusing a buffer. Currently the array module can be used for
this but I would like to get rid of it in favor of bytes and Travis
Oliphant's new buffer API (which serves a similar purpose as the array
module but has a much more powerful mini-language to describe the
internal structure of the elements, similar to the struct module.)
--Guido van Rossum (home page: http://www.python.org/~guido/)
More information about the Python-3000