I was working with inheritance in WTForms.
I had next class:
class MyForm(WTForms):
...
def process(self, formdata=formdata, obj=None):
super(self.__class__, self).process(formdata=formdata, obj=obj)
And I had error (not always, but in some cases with similar forms – it is even more strange):
Maximum recursion depth have been exceeded
But when I’ve changed self.__class__ → MyForm everything was OK!
I don’t get it… As I know they must be synonyms. What’s wrong?
They are not synonyms.
selfmay be any object of a class that inherits fromMyForm. Python does not silently create a new superclass object for calling inherited methods, it just passes the same object (doing so would break polymorphism and serve no use). And thetype()/.__class__is obviously the class the object is really an instance of, not some superclass of that (you don’t expectMyShinyThing().__class__to yieldobject, right?). It would be really astonishing, non-idiomatic and useless if__class__changed depending on where it is accessed from. It’s just polymorphism.And as the first argument to
superis an indicator where in the MRO the search for a supermethod should continue (informally: where you currently are), passingself.__class__always starts back at the (immediate) superclass of whatever classselfis an instance of. Assuming propersupercalls elsewhere in the class hierarchy, this ultimately gets you back toMyForm.process, and we’ve got our infinite loop.