Force base method call

There isn’t and shouldn’t be anything to do that. The closest thing I can think of off hand if something like having this in the base class: public virtual void BeforeFoo(){} public void Foo() { this.BeforeFoo(); //do some stuff this.AfterFoo(); } public virtual void AfterFoo(){} And allow the inheriting class override BeforeFoo and/or AfterFoo

How does multiple inheritance work with the super() and different __init__() arguments?

For question 2, you need to call super in each class: class First(object): def __init__(self): super(First, self).__init__() print “first” class Second(object): def __init__(self): super(Second, self).__init__() print “second” class Third(First, Second): def __init__(self): super(Third, self).__init__() print “that’s it” For question 3, that can’t be done, your method needs to have the same signature. But you could … Read more

Require override of method to call super

There’s no way to require this directly. What you can do, however, is something like: public class MySuperclass { public final void myExposedInterface() { //do the things you always want to have happen here overridableInterface(); } protected void overridableInterface() { //superclass implemention does nothing } } public class MySubclass extends MySuperclass { @Override protected void … Read more

In python, super() is always called first in a method. Are there situations where it should be called later?

Sometimes you need to validate the arguments before calling super(): class UpperBase(Base): def __init__(self, name): if not name_valid(name): raise ValueError() super(UpperBase, self).__init__(name) I don’t see why this wouldn’t be pythonic, because it’s the easiest way to do it and it’s straightforward. Also, read @JHSaunders’ comment, he makes a good point.

“MetaClass”, “__new__”, “cls” and “super” – what is the mechanism exactly?

OK, you’ve thrown quite a few concepts into the mix here! I’m going to pull out a few of the specific questions you have. In general, understanding super, the MRO and metclasses is made much more complicated because there have been lots of changes in this tricky area over the last few versions of Python. … Read more

Python: ‘super’ object has no attribute ‘attribute_name’

After the base class’s __init__ ran, the derived object has the attributes set there (e.g. some_var) as it’s the very same object as the self in the derived class’ __init__. You can and should just use self.some_var everywhere. super is for accessing stuff from base classes, but instance variables are (as the name says) part … Read more