1

In python, what is super(classItSelf) doing?

I have a project to work on has code such as this:

class Tele(TcInterface):

    _signal = signal.SIGUSR1

    def __init__(self):
        super(Tele, self).__init__()
        self._c = None

What is this line doing?

super(Tele, self).__init__()

I am a cpp java code, Really got confused about python stuff.

Gordon
  • 19,811
  • 4
  • 36
  • 74
BufBills
  • 8,005
  • 12
  • 48
  • 90
  • What questions do you have that are not answered by [the documentation](https://docs.python.org/2/library/functions.html#super)? – kindall Mar 13 '15 at 21:10
  • ```Tele``` is a subclass of ```TcInterface```. ```Tele``` overrides ```__init___``` and ```super(Tele, self).__init__()``` executes ```__init___``` in ```TcInterface``` which is ```Tele```'s parent (super). If you want to *extend* a method that was inherited, you can use ```super``` to execute that method from the parent then add more instructions/statements to extend it. – wwii Mar 13 '15 at 21:11
  • Similar to, if not duplicate of, [Understanding Python super() with __init__() methods](http://stackoverflow.com/q/576169/2823755) – wwii Mar 13 '15 at 21:21

2 Answers2

1

This calls the __init__() function (constructor) of the super/parent class (TclInterface). This is often necessary, as the superclass constructor is otherwise overridden by __init__() in the subclass.

aganders3
  • 5,838
  • 26
  • 30
1

you need to pass at least self to __init__.

python is different from some traditional OOP languages, which will handle self or this or whatever name you give to the current instance automatically for you.

in python, self is exposed, even in constructor, so that you need to pass in and reference it yourself. and this super(...) here, it basically looks for a proxy, which represents all super classes, and super(...).__init__ is referencing the constructors from the super classes. it's the same concept as you call super() in C++ or Java.

please reference doc for more: https://docs.python.org/2/library/functions.html#super

Jason Hu
  • 6,239
  • 1
  • 20
  • 41