Discussion:
Subclassing PersistentList
Patrick Dobbs
2009-10-13 13:07:19 UTC
Permalink
Hi,

I've a class which subclasses PersistentList and which has a mixin:

class Confused(PersistentList,MyMixin):

def __init__(self,*args,**kwds):
#init stuff
super(Confused,self).__init__(*args,**kwds)


Previously, it subclassed PersistentObject and all worked fine. When I
changed it to subclass PersistentList I found that that MyMixin was not
initialising. This seems to be because PersistentList defines an
__init__ method which doesn't call super() whereas PersistentObject
doesn't define an __init__ method at all.

This seems to be fixed by the addition of:

super(PersistentList,self).__init__(*args,**kwargs)

on line 18 in PersistentList.

Presumably the same issue would occur with PersistentDict.

Is this behaviour by design, or would it be a good idea to add these
super() inits calls to the Persistent classes so they play well with
subclasses?

Thanks very much, by the way, for developing and maintaining durus. It's
a pleasure to work.

Regards

Patrick
Binger David
2009-10-15 00:52:57 UTC
Permalink
Hi Patrick,

These container classes are not intended to be subclassed at all,
and we never do that in our own applications.

If we added the super() calls, we might possibly break other people's
subclasses that depend (unfortunately) on not calling the next
__init__() in the inheritance tree. Is it worth this risk?

When we use Mixins in other contexts, we just call the
parent __init__() methods explicitly so that the behavior is
easier to understand. Would that work as well for you?

And I'm glad you like Durus! We like it too.

- David
Patrick Dobbs
2009-10-15 08:35:45 UTC
Permalink
Thanks for that.

I figured out that subclassing the container methods is problematic
since they override the comparison methods (e.g. __eq__) to delegate
calls to the wrapped List property (data). Given that I agree that
there's no benefit in adding super() calls.

Regards

Patrick
Post by Binger David
Hi Patrick,
These container classes are not intended to be subclassed at all,
and we never do that in our own applications.
If we added the super() calls, we might possibly break other people's
subclasses that depend (unfortunately) on not calling the next
__init__() in the inheritance tree. Is it worth this risk?
When we use Mixins in other contexts, we just call the
parent __init__() methods explicitly so that the behavior is
easier to understand. Would that work as well for you?
And I'm glad you like Durus! We like it too.
- David
Loading...