0

This is my class structure:

class MyMixin(object):
    def __init__(self, **kwargs):
        super(MyMixin, self).__init__(**kwargs)

class MyBaseView(MyMixin, TemplateView):
    def __init__(self, **kwargs):
        print 'MyBaseView init'
        super(MyBaseView, self).__init__(**kwargs)

class MyCommonView(MyBaseView):
    def __init__(self, **kwargs):
        print 'MyCommonView init'
        super(MyCommonView, self).__init__(**kwargs)

class MyView(MyCommonView):
    def __init__(self, **kwargs):
        print 'MyView init'
        super(MyView, self).__init__(**kwargs)

In urls.py:

url(r'^some/url/$', MyView.as_view())

Also, there are some instance variables defined in each constructor. I didn't write them here because I don't think they are relevant.

Result... MyView and MyCommonView init messages get printed, but MyBaseView doesn't. So, MyBaseView's constructor never gets called. I know for a fact the constructor isn't called because I see some things are not initialized properly, prints are here just to demonstrate they are not called.

Why? What could be causing this? How to resolve it?

Thanks.

morgoth84
  • 1,070
  • 2
  • 11
  • 25
  • The structure, as posted here, will most certainly invoke `MyBaseView.__init__()`. Are you certain that this sample reflects your actual project setup accurately? – Martijn Pieters Jun 27 '14 at 12:20
  • 5
    The `TemplateView.__init__()` method will *not* be called unless a `super()` call is added to `MyMixin.__init__()`. – Martijn Pieters Jun 27 '14 at 12:22
  • Sorry, I've overlooked super in MyMixin, it is there. And thanks for the edit. But yes, this is my class structure. There is some more logic in each constructor, but super should be executed each time. – morgoth84 Jun 27 '14 at 12:48
  • BTW: `__init__` is not the constructor, the `__new__` method is it. `__init__` is only the initializer which is called after object creation. – Kiwisauce Jun 27 '14 at 15:10
  • I'm using the term "constructor" in a generic OOP way, not related to any particular language. In this case, the __init__ method is the one which resembles a constructor the most. This answer also confirms this: http://stackoverflow.com/questions/674304/pythons-use-of-new-and-init#answer-8665179 – morgoth84 Jul 01 '14 at 07:39

1 Answers1

0

Ok, this was weird as hell, but it's resolved now.

I had some of these classes inside a submodule in my project. This submodule was removed from the project to a standalone app (installable in virtualenv via pip). I did install it locally and I did pull the latest revision of my project from revision control, the revision which doesn't include this submodule any more.

Long story short, the submodule was removed, but its directory remained along with some .pyc files inside. These file were outdated and my project was using them for some reason and this is why it didn't work.

d33tah
  • 10,999
  • 13
  • 68
  • 158
morgoth84
  • 1,070
  • 2
  • 11
  • 25