How to use super() correctly?

Asked

Viewed 69 times

2

I’m a beginner and I’m trying to learn the Tkinter framework, but I have a doubt now that is the use of super(), I know that the super is a way to inherit the class "main" to "subclasses", but in the example of Tkinter is used super() right in the class "main", follow the example code.

import tkinter as tk

class Application(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.master = master
        self.pack()
        self.create_widgets()


    def create_widgets(self):
        self.hi_there = tk.Button(self)
        self.hi_there["text"] = "Hello World\n(click me)"
        self.hi_there["command"] = self.say_hi
        self.hi_there.pack(side="top")

        self.quit = tk.Button(self, text="QUIT", fg="red",
                                command=self.master.destroy)
        self.quit.pack(side="bottom")

    def say_hi(self):
        print("hi there, everyone!")

root = tk.Tk()
app = Application(master=root)
app.mainloop()

Why use super() in this case?

  • 1

    I think that Application in this case uses as super class tk.Frame. So the use of the word super within the class is always a reference to tk.Frame. When one class is not inheriting anything from another, the class declaration statement is just class Nome:

1 answer

1


This doubt of yours has nothing to do with Tkinter, it has to do with object orientation. Come on.

The theory is important

class A():
    def metodo1(self):
        print('metodo1 da classe A')

class B(A):
    def metodo2(self):
        print('metodo2 da classe B')

In the code above, the class B was declared as subclass of the A. This means that all methods and properties declared in A are also part of the B.

If you create a class object B can call the metodo1.

obj_b = B()
obj_b.metodo1()

It will print "class A methodo1", even though you instantiated a class object B. That’s exactly what the class inheritance system is for.

Now imagine you want to declare a method in the class B whose name already exists in the class A. What’s going to happen?

class A():
    def metodo1(self):
        print('metodo1 da classe A')

class B(A):
    def metodo2(self):
        print('metodo2 da classe B')

    def metodo1(self):
        print('metodo1 da classe B')

obj_b = B()
obj_b.metodo1()

It will print "method 1 of class B". When there are two methods with the same name, Python chooses what was declared in the class itself.

If you need to access a superclass method whose name has already been used in the subclass, you should use the super().

class A():
    def metodo1(self):
        print('metodo1 da classe A')

class B(A):
    def metodo2(self):
        print('metodo2 da classe B')

    def metodo1(self):
        print('metodo1 da classe B')

    def metodo3(self):
        super().metodo1()

obj_b = B()
obj_b.metodo3()

It will print "Class A methodo1", because the método3 uses the super() to call directly the superclass.

Recapping

You declared the class Application as subclass of tk.Frame.

class Application(tk.Frame):

And the class Application already declares a method called __init__ (who is your builder).

    def __init__(self, master=None)

That’s why you need the super() to call the method __init__ class tk.Frame.

super().__init__(master)

Otherwise the constructor would call himself and, consequently, enter an infinite loop.

I recommend studying object orientation before frameworks. Things will make more sense.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.