[Python-Dev] python and super

Steven D'Aprano steve at pearwood.info
Fri Apr 15 03:23:52 CEST 2011


Ricardo Kirkner wrote:

> I have a TestCase class, which inherits from both Django's TestCase
> and from some custom TestCases that act as mixin classes. So I have
> something like
> 
> class MyTestCase(TestCase, Mixin1, Mixin2):
>    ...
> 
> now django's TestCase class inherits from unittest2.TestCase, which we
> found was not calling super. Even if this is a bug and should be fixed
> in unittest2, this is an example where I, as a consumer of django,
> shouldn't have to be worried about how django's TestCase class is
> implemented. Since I explicitely base off 3 classes, I expected all 3
> classes to be initialized, and I expect the setUp method to be called
> on all of them.
> 
> If I'm assuming/expecting unreasonable things, please enlighten me.

If we treat django's failure to use super as a bug, you want the Python 
language to work-around that bug so that:

"I, as a consumer of django, shouldn't have to be worried about bugs in 
django". (For at least one class of bug.)

If we *don't* treat django's failure to use super as a bug, but as a 
deliberate design choice, then you are trying to do something which 
django doesn't support. Possibly *deliberately* doesn't support. You 
want the Python language to add that support so that:

"I, as a consumer of django, shouldn't have to be worried about whether 
django supports what I want to do or not".

Either way you look at it, I think it's extremely unreasonable to expect 
the language to work-around bugs in third-party applications, or to add 
features to them that the third-party developers either didn't consider 
or don't want.

Multiple inheritance is tricky enough to get right without adding "Do 
What I Mean" black magic to it. I'd rather work around bugs in 
third-party classes than try to deal with Python actively subverting the 
code I read and write by mysteriously calling superclass methods where 
there is no call to a superclass method.




-- 
Steven


More information about the Python-Dev mailing list