Weakrefs to classes that derive from str

Raymond Hettinger vze4rx4y at verizon.net
Thu Mar 31 04:30:09 CEST 2005


[Ron Garret]
> Thanks for the detailed explanation.  I understand now why you can't
> create weakrefs to these types.  What I don't understand still is why
> you can't create weakrefs to user-defined classes that inherit from
> these types.  I would think that instances of user-defined classes have
> the same header structure regardless of what they inherit from.  This
> would seem to be supported by the fact that you can create weakrefs to
> instances of user-defined classes that inherit from int and float.

It is an over-statement to say that it can't be done.  In fact, Michael Hudson
is already working on a patch.

It is more accurate to say that the current mechanism doesn't allow it.
Michael's solution is to build a new mechanism.

The existing mechanism has a subclass extend the superclass's structure:

  [--someobj--][--subclassdata--]
                                       ^
                                       |
  |---- offset to wr table ---

The offset is fixed for the type and must be the same across instances.

This is a problem for tuples and ints because someobj is of varying length:

 [--tuple header, elem0, elem1, elem2--]
 [--tuple header, elem0 ]

In contrast, ints and floats floats have no problem because they are always the
same size:

 [--int header, int value--]


Raymond Hettinger









More information about the Python-list mailing list