Hi forum,
I see the following code on an post which posted two years ago. It stated that the code is from the book “Python GUI Programming with Tkinter”, p.76. I don’t have the book.
I wonder, the Line 11 call super().display()
in class LoggerMixin. The class does not inherit any other class except the common object class. How can it use super() and eventually super().display() leads to Displayer.display()?
And the LoggerMixin class calling an inexistent makes the Mixin concept?
So many new in Python for me!
Thanks
class Displayer:
def display(self, message): # Line 2
print(message)
class LoggerMixin:
def log(self, message, filename='logfile.txt'):
with open(filename, 'a') as fh:
fh.write(message)
def display(self, message):
super().display(message) # Line 11, calls Line 2
self.log(message)
class MySubClass(LoggerMixin, Displayer):
def log(self, message):
super().log(message, filename='subclasslog.txt')
subclass = MySubClass()
subclass.display('This string will be shown and logged in subclasslog.txt')
This is the MRO:
print(MySubClass.__mro__)
(<class '__main__.MySubClass'>, <class '__main__.LoggerMixin'>, <class '__main__.Displayer'>, <class 'object'>)