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
Cœur
  • 37,241
  • 25
  • 195
  • 267

1 Answers1

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
Mariusz Jamro
  • 30,615
  • 24
  • 120
  • 162
  • 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 http://stackoverflow.com/questions/1401661/python-list-all-base-classes-in-a-hierarchy I will be able to call __init__ functions of all base classes. Great work! – user1214179 Feb 18 '12 at 12:44