The last Method

Hello! I’m having problems with the last method/function, I can’t really understand what is there for? What does it do ?

from abc import ABC, abstracmethod

class UIControl(ABC):
def draw(self):
pass

class TextBox(UIControl):
def draw(self):
print(“TextBox”)

class DropDwonList(UIControl):
def draw(self):
print(“TextBox”)

def draw(control):
control.draw()

This looks like an example of polymorphism. So what the last function does is to show how cool polymorphism is. You can send it a TextBox or a DropDownList as the control parameter and then just call the draw method on this control parameter and then either the TextBox or the DropDownList will draw itself. def draw does not have to know if it is a Textbox or a DropDownList.

The code could look something like this:

from abc import ABC, abstracmethod

class UIControl(ABC):
    def draw(self):
        pass

class TextBox(UIControl):
    def draw(self):
        print(“TextBox”)

class DropDownList(UIControl):
   def draw(self):
       print(“TextBox”)

def draw(control):
    control.draw()

text_box = Textbox()
dd_list = DropDownList()

draw(text_box)
draw(dd_list)

The last two statements will draw a TextBox and a DropDownList respectively without draw having to know what the really are.

Cool!

First of all thank you again for your reply. Second, I’m reading your explanation but I just can’t understand it yet. With the last draw function/method you can call the TextBox or DropDownList class as the parameters of the draw function ?