0

Here is a rough idea of what I want to do:

class PlayerModel(Timeline):
    def __init__(self, name):
        self.name = name

        self.timeline_min = super(timeline_min)     # I don't know the syntax for this
        self.timeline_max = super(timeline_max)     # I don't know the syntax for this


class Timeline:
    def __init__(self):
        self.timeline_min = 0
        self.timeline_max = 120

    def make_model(self, m_name):
        return PlayerModel(m_name)

I want PlayerModel to have the same properties from Timeline which are:

self.timeline_min = 0
self.timeline_max = 120

in it's __init__, without having to pass them as parameters. Could I achieve this using super()? I could not find a way to do this with the parent's variables.

Stidgeon
  • 2,673
  • 8
  • 20
  • 28
pylab
  • 77
  • 1
  • 6
  • Does this answer your question? [What does 'super' do in Python?](https://stackoverflow.com/questions/222877/what-does-super-do-in-python) – azro Dec 25 '19 at 22:11

1 Answers1

5

You should let Timeline set them by calling its __init__:

class PlayerModel(Timeline):
    def __init__(self, name):
        super().__init__()
        self.name = name

Unless you explicitly call the parent constructor in the child __init__ method, Python just assumes you just want to override the parent constructor instead.

Anonymous
  • 11,748
  • 6
  • 35
  • 57
  • This works, but is there any way I can do this with `super()` but only for specific variables from `Timeline`? Say I had a bunch of variables in `Timeline` but only want `PlayerModel` to inheret `timeline_min` and `timeline_max`. As far as I know this inherits all variables from the parent class – pylab Dec 25 '19 at 22:17
  • @pylab The short answer is no. Usually that only makes sense if the parent `__init__` takes arguments. Otherwise you would be constructing an object in a way the super class doesn't support. You can change the variables after you call the super method if you want. – Anonymous Dec 25 '19 at 22:21