简体   繁体   中英

In python, what is super(classItSelf) doing?

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM