1

I'm a beginner at Python and I'm currently on chapter 10/12 in my book 'Python Programming for the absolute beginner'. I understand OOP in Python but due to one chapter (which discusses some relevant information about OOP) being based around a program for playing 'Cards' I had to skip part of the chapter (because I don't know how to play Cards) and so I missed out on important information which I should know at this point in the book. The reason I'm saying this is because I will need a simple answer to my question because complex answers may be too difficult for me to interpret.

But anyway here is my problem, there is a piece of a code in my book which creates a simple GUI program (which I completely understand) and then there's an Object Oriented version of the program which I do not understand (main parts highlighted in '##'s). It contains something called a 'Superclass constructor' which completely confused me (I tried doing some research on it, but it just didn't make sense to me). If anyone can help explain to me how the second version of the code works (or give helpful resources) then I will be highly grateful:

First version of the code:

from tkinter import *

# create a root window
root = Tk()
root.title("Lazy Buttons")
root.geometry("200x85")

app = Frame(root)
app.grid()

bttn1 = Button(app, text = "This is a button")
bttn1.grid()

root.mainloop()

Second version:

from tkinter import *

class Application(Frame):

    def __init__(self, master): # Why is 'master' called?
        super(Application, self).__init__(master)    # ?
        self.grid()
        self.create_widgets()

    def create_widgets(self):
        self.bttn1 = Button(self, text = "This is a button")
        self.bttn1.grid()

root = Tk()
root.title("Lazy Buttons 2")
root.geometry("200x85")
app = Application(root)
root.mainloop()
Ihor Kaharlichenko
  • 5,944
  • 1
  • 26
  • 32
  • 1
    Don't use asterisks (*) to highlight Python code parts, * is a part of a language, though brings confusing for those who reads your code, you'd better use comments (#) to explain your doubts in the code. – Rostyslav Dzinko Aug 17 '12 at 11:06
  • Now those parts of the code are commented out. Rostyslav meant that you should add explanatory comments, not emphasize parts of your code. – Kris Harper Aug 17 '12 at 11:11
  • 1
    What you've done is OK, but that's not what Rostyslav meant: You should post working code, which means that you should use "#" as a *comment* marker, and say something like `app = Application(root) ## what does this mean?` – Andrew Jaffe Aug 17 '12 at 11:12
  • 1
    Don't worry I changed it –  Aug 17 '12 at 11:13
  • 1
    Voting to close as exact duplicate. From OP's comment to [this answer](http://stackoverflow.com/a/12004693/623518) it seems that the question is basically what is the use of `super` for. This is covered in the Python [documentation](http://docs.python.org/library/functions.html#super) and in the question [Understanding Python super() and init methods](http://stackoverflow.com/questions/576169/understanding-python-super-and-init-methods) – Chris Aug 17 '12 at 11:37
  • I did ask specifically for a simple answer and I did mention already I try to do some research on the superclass function but didn't get a simple solution. The documentation wasn't much help. –  Aug 17 '12 at 14:49

2 Answers2

1

Don't skip parts of a tutorial because you don't know the problem domain - you don't need to know how to play cards to understand how card game code relates to what the program does. To your actual problem:

class Application(**Frame**):

This creates a class Application that inherits from a class Frame. If you don't know inheritance, the tutorial you're following should explain it, or you could try this introduction to classes and inheritance that I just googled up.

**def __init__(self, master):

This creates a method, with the name __init__. This is a special method in Python, which behaves similarly to a constructor in other languages - essentially, whenever an Application is created, Python will immediately call its __init__ method before it gives the new object back to whoever made it. master is just an argument, same as any other to any other function.

super(Application, self).__init__(master)

This calls the constructor of the superclass, to let it initialise the new object. super(Application, self) figures out what the superclass is (in this case it is Frame; in more complex cases that you will come to eventually, this is harder to work out and super's magic becomes important).

self.create_widgets()**

This calls the method create_widgets, which you define below this. The object before the . gets passed into the method as the first argument, self - so, this calls a different method on the same object that the method you're in has been called on.

app = Application(root)

This creates an Application - objects are created by calling the class, like how list() creates a list. The argument you pass in gets handed to __init__ as its second argument (the new Application that Python creates behind the scenes gets passed as the first argument, self, as above). Python creates a new Application, calls its __init__ for you, and then assigns the new object to the name 'app'.

lvc
  • 34,233
  • 10
  • 73
  • 98
  • Thanks for the answer, sorry if I made my problem unclear but the main part of the code which confuses me is the 'Super' part of the __init__ constructor, I know how to use the __init__ constructor I just never seen the whole 'Super' thing been used before –  Aug 17 '12 at 11:17
  • 1
    See the super [documentation](http://docs.python.org/library/functions.html#super) and this related question: http://stackoverflow.com/questions/576169/understanding-python-super-and-init-methods – Chris Aug 17 '12 at 11:35
0

'super' part here is only to properly call constructor of base class (the class you are inheriting from.) Let's consider following example:

class Device():  # class representing some drawing device
    ...
    def fill(self, color)
        ...  # actual code for draw on this device

screen = Device()
WHITE_COLOR = (255, 255, 255)

class Window(object):
    def __init__(self, width, height):
        self.width  = width
        self.height = height

    def area(self):
        return self.width * self.height

class ColoredWindow(Window):
    def __init__(width, height, color):
        super(ColoredWindow, self).__init__(width, height)
        self.color = color

    def fill(self):
        screen.fill(self.width, self.height, self.color)

my_window = ColoredWindow(640, 480, WHITE_COLOR)
my_window.fill()

The first class here, Window, is a generic window that has width and height attributes that are passed in constructor. ColoredWindow is a subscass of Window, it has additional attribute color. When ColoredWindow is constructed, it needs somehow pass width and height parameters to its baseclass Window.

This is exactly what construction super(ColoredWindow, self).__init__(width, height) is for. The first part, super(ColoredWindow, self), returns a reference to baseclass (Window in our case), and second part, __init__(width, height), just calls its constructor with right parameters.

In your example master is not called, it's just parameter to __init__ method of Application class.

Palasaty
  • 5,181
  • 1
  • 26
  • 22
  • Thanks for your answer, it certainly helped me understand the coding –  Aug 17 '12 at 22:32