[Python-3000] bytes & Py_TPFLAGS_BASETYPE

Guido van Rossum guido at python.org
Mon Sep 17 19:53:58 CEST 2007


On 9/17/07, Mathieu Fenniak <mathieu.fenniak at gmail.com> wrote:
> On 17-Sep-07, at 9:00 AM, Guido van Rossum wrote:
> > 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?
>
> The builtin type subclasses in pyPdf (including the would-be bytes
> subclass) add additional methods that every pdf object is expected to
> support.  All the PDF object types have two additional methods
> (writeToStream and getObject) that have varying behavior for each
> class: (relatively inconsequential PDF information follows)
>
> "writeToStream" method that serializes the object -- a byte string
> would write out <68656c6c6f>, a text string (hello), and so on for
> other more complex types (dictionaries, labels, arrays, PDF data
> streams).  The type is also responsible for encrypting itself when
> applicable.

This sounds like a perfect application for generic functions instead
of subclassing.

> PDF files also have an ability to reference objects elsewhere in the
> file.  For example, the length of a content stream can be a simple
> "500 bytes", or it can be "read this length at offset X in the
> file".  Since almost any object can be an indirect object reference,
> the library objects support a "getObject" method that returns self --
> excluding PDF "indirect object reference" objects, which read an
> object from a table in a PDF file.

Similar.

> If you decide that bytes should be subclassable, I've included with
> this e-mail a patch that adds the basetype bit, adds some unit tests
> for bytes subclasses, and includes __dict__ in the bytes_reduce
> method (for pickling subclass instances).  I was going to upload this
> to the SF patch manager, but it appears to be closed to permit only
> project members access.
>
> Mathieu Fenniak
>
>
>


-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)


More information about the Python-3000 mailing list