[Python-3000] bytes & Py_TPFLAGS_BASETYPE
Guido van Rossum
guido at python.org
Mon Sep 17 17:00:20 CEST 2007
On 9/16/07, Mathieu Fenniak <mathieu.fenniak at gmail.com> wrote:
> On 16-Sep-07, at 7:56 PM, Guido van Rossum wrote:
> > So suppose my answer was "no, bytes won't be subclassable". How much
> > would you really lose by having to wrap a separate object around a
> > bytes object, rather than being able to subclass? How much extra code
> > do you think you would have to write?
> > Another way to look at it-- how much of the bytes type's API do your
> > objects really have to support?
> Most often, I'd be concatenating and comparing with other bytes
> objects, iterating through the byte array, and passing the byte array
> into methods like stream.write. Iterating and comparing could be
> dealt with by some code in the containing class; for other needs, I
> would sprinkle ".data" property accesses throughout the code to
> access the bytes instance.
OK, so it sounds like at least you are treating it as a bytes array.
> I'm not too concerned about the programming I'd have to do, even
> though the end result wouldn't really be what I'd like to have. It's
> not the end of the world, it's just not ideal.
> I do think that subclassing bytes would probably be a request a few
> people would have, especially when porting Python 2 code that
> subclasses str.
Well, due to bytes' mutability, they'll be in for a ton of surprises.
> It seems especially unusual that bytes can't be
> subclassed, when builtin types like str, list, dict, and set can be.
Maybe I should apologize for pushing back so hard, but in my
experience most people who subclass a built-in type do it because they
can, not because they should -- the lamented "path" module being a
prime example in my view.
I'm still not convinced of the usefulness in your case -- what would
you lose if you just passed a bytes instance around instead of an
instance of the subclass you'd like to have?
--Guido van Rossum (home page: http://www.python.org/~guido/)
More information about the Python-3000