1

Why this code doesn't work? I see in debugger (PyCharm) that init line is executed but nothing more. I have tried to put there raise exception to be really sure and again nothing happend.

class polo(object):
    def __init__(self):
        super(polo, self).__init__()
        self.po=1     <- this code is newer executed

class EprForm(forms.ModelForm, polo):
    class Meta:
        model = models.Epr
2
  • Why doesn't it work? What errors etc are you getting? Commented Feb 16, 2012 at 16:14
  • Absolutely none errors. The code is simply not executed. Commented Feb 17, 2012 at 0:08

1 Answer 1

2

You use multiple inheritance so in general Python will look for methods in left-to-right order. So if your class do not have __init__ it'll look for it in ModelForm and that (only if not found) in polo. In your code the polo.__init__ is never called because ModelForm.__init__ is called.

To call the constructors of both base classes use explicit constructor call:

class EprForm(forms.ModelForm, polo):

    def __init__(self, *args, **kwargs)
        forms.ModelForm.__init__(self, *args, **kwargs) # Call the constructor of ModelForm
        polo.__init__(self, *args, **kwargs) # Call the constructor of polo

    class Meta:
        model = models.Epr
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very mach. It's logical now, when I see your answer. I don't know why a supposed that init is some special method and will by called for every inherited class. And with knowledge from stackoverflow.com/questions/1401661/… I will be able to call init functions of all base classes. Great work!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.