3

I have the following code, in which I am trying to extend the init method of BaseExporter:

from apps.ingest.platform_export import BaseExporter

class Vudu(BaseExporter):

    def __init__(self):
        BaseExporter.__init__()
        self.platform = ' Vudu'

Basically, I would like all of the init'd variables from the BaseExporter plus the additional variable of self.platform = 'Vudu'. How would I do this correctly?

David542
  • 104,438
  • 178
  • 489
  • 842

2 Answers2

8

Python 3

from apps.ingest.platform_export import BaseExporter

class Vudu(BaseExporter):
    def __init__(self):
        super().__init__()
        self.platform = ' Vudu'

Python 2

from apps.ingest.platform_export import BaseExporter

class Vudu(BaseExporter):
    def __init__(self):
        super(Vudu, self).__init__()
        self.platform = ' Vudu'
Dušan Maďar
  • 9,269
  • 5
  • 49
  • 64
5

You were on the right track, only missing self on the parent class

from apps.ingest.platform_export import BaseExporter

class Vudu(BaseExporter):

    def __init__(self):
        BaseExporter.__init__(self)
        self.platform = ' Vudu'
Fran Lupión
  • 134
  • 10
  • 2
    I believe using super is "more" correct - as the code will work even if the classes are refactored for multiple inheritance for instance. – Tony Suffolk 66 Jun 02 '15 at 20:35
  • I prefer to use the parent for each __init__ because it could have more than one argument even more with multiple inheritance – Fran Lupión Jun 02 '15 at 20:44
  • 1
    in which case you should use variable argument lists. Using super ensures that the Inheritance tree is always executed in the correct way. – Tony Suffolk 66 Jun 02 '15 at 20:54