subba
subba

Reputation: 1695

Diamond problem and puzzling output of 'super' keyword usage

class CheckMaintenanceMode:
    def validate_form(self):
        print("CheckMaintenanceMode: Before super")
        super().validate_form()
        print("CheckMaintenanceMode: After super")

class CreateAudit:
    def validate_form(self):
        print("CreateAudit: Before super")
        super().validate_form()
        print("CreateAudit: After super")
        print("Create audit record")

class HandleForm:
    def validate_form(self):
        print("Validate form")
        print("Save data to Database")

class ProcessOnboarding(HandleForm):
    def validate_form(self):
        print("ProcessOnboarding: Before super")
        super().validate_form()
        print("ProcessOnboarding: After super")
        print("Onboarding complete. Redirect to main page")

class CustomActionProcessOnboarding(CreateAudit, CheckMaintenanceMode, ProcessOnboarding): 
    def validate_form(self):
        print("CustomActionProcessOnboarding: Before super")
        super().validate_form()
        print("CustomActionProcessOnboarding: After super")
        print("This is a particular action this class performs at the end")
        
if __name__ == "__main__":
    CustomActionProcessOnboarding().validate_form()

Output:

CustomActionProcessOnboarding: Before super
CreateAudit: Before super
CheckMaintenanceMode: Before super
ProcessOnboarding: Before super
Validate form
Save data to Database
ProcessOnboarding: After super
Onboarding complete. Redirect to main page
CheckMaintenanceMode: After super
CreateAudit: After super
Create audit record
CustomActionProcessOnboarding: After super
This is a particular action this class performs at the end

I need help in explaining the output. It seems like there is an interplay of MRO C3 linearization and usage of super().

Upvotes: 0

Views: 27

Answers (0)

Related Questions