In the following code:
import tkinter as tk
class CardShuffling(tk.Tk):
background_colour = '#D3D3D3'
deck_size = 52
def __init__(self):
tk.Tk.__init__(self) # What does this do?
I am confused about the purpose of the last line.. an instance of CardShuffling inherits from tk.Tk
, doesn't the last line just repeat what is already taken care of by CardShuffling(tk.Tk)
??
CardShuffling(tk.Tk)
only makes the class CardShuffling
a child of tk.Tk
. your new class inherits all the methods of this class.
but if you create a new object you still have to call the constructor of that base class (with the new object as argument). imagine there is a line self.a = 0
in the constructor of the parent class. your child class has to run this line when you initialize a new instance; CardShuffling(tk.Tk)
can not do that for you; you need to run __init__
of the parent class.
the usual way to do that in python 3 is
def __init__(self):
super().__init__()
which is the same in this case as
def __init__(self):
tk.Tk.__init__(self)
maybe this article on inheritance helps and there is even a book chapter freely available.