|
| 1 | +=================== |
| 2 | +Abstract classes |
| 3 | +=================== |
| 4 | + |
| 5 | +Abstract classes in Object Oriented Programming (OOP) are classes that cannot be instantiated. |
| 6 | +In Python, abstract classes can be by inheriting the :class:`abc.ABC` class. Abstract classes also need to have |
| 7 | +abstract methods, otherwise Python will not treat the class as abstract. Trying to create an instance (object) |
| 8 | +from an abstract class results in a TypeError exception. |
| 9 | + |
| 10 | +.. code-block:: python |
| 11 | +
|
| 12 | + from abc import ABC, abstractmethod |
| 13 | +
|
| 14 | + class MyAbstractClass(ABC): |
| 15 | + @abstractmethod |
| 16 | + def some_method(self): |
| 17 | + pass |
| 18 | +
|
| 19 | + class MyClass(MyAbstractClass): |
| 20 | + def some_method(self): |
| 21 | + return 5 * 5 |
| 22 | +
|
| 23 | + # OK |
| 24 | + my_instance = MyClass() |
| 25 | +
|
| 26 | + # TypeError: Can't instantiate abstract class MyClass with abstract method some_method |
| 27 | + my_abstract_instance = MyAbstractClass() |
| 28 | +
|
| 29 | +
|
| 30 | +Let's modify our example from :ref:`Polymorphism`. |
| 31 | + |
| 32 | +.. code-block:: python |
| 33 | + :linenos: |
| 34 | + :emphasize-lines: 1, 8, 12 |
| 35 | +
|
| 36 | + from abc import ABC, abstractmethod |
| 37 | +
|
| 38 | + import tkinter as tk |
| 39 | + import tkinter.ttk as ttk |
| 40 | + import tkclasswiz as wiz |
| 41 | +
|
| 42 | + # An abstract class |
| 43 | + class Wheel(ABC): |
| 44 | + def __init__(self, diameter: float): |
| 45 | + self.diameter = diameter |
| 46 | +
|
| 47 | + @abstractmethod |
| 48 | + def get_info(self) -> str: |
| 49 | + pass |
| 50 | +
|
| 51 | + class WinterWheel(Wheel): |
| 52 | + def get_info(self) -> str: |
| 53 | + return "Wheel for winter." |
| 54 | +
|
| 55 | + class SummerWheel(Wheel): |
| 56 | + def get_info(self) -> str: |
| 57 | + return "Wheel for summer." |
| 58 | +
|
| 59 | +
|
| 60 | + class Car: |
| 61 | + def __init__(self, name: str, speed: float, wheels: list[Wheel]): |
| 62 | + self.name = name |
| 63 | + self.speed = speed |
| 64 | + self.wheels = wheels |
| 65 | +
|
| 66 | + if speed > 50_000: |
| 67 | + raise ValueError("Car can go up to 50 000 km / h") |
| 68 | +
|
| 69 | + if len(wheels) != 4: |
| 70 | + raise ValueError("The car must have 4 wheels!") |
| 71 | +
|
| 72 | + # Tkinter main window |
| 73 | + root = tk.Tk("Test") |
| 74 | +
|
| 75 | + # Modified tkinter Combobox that will store actual objects instead of strings |
| 76 | + combo = wiz.ComboBoxObjects(root) |
| 77 | + combo.pack(fill=tk.X, padx=5) |
| 78 | +
|
| 79 | + def make_car(old = None): |
| 80 | + """ |
| 81 | + Function for opening a window either in new definition mode (old = None) or |
| 82 | + edit mode (old != None) |
| 83 | + """ |
| 84 | + assert old is None or isinstance(old, wiz.ObjectInfo) |
| 85 | +
|
| 86 | + window = wiz.ObjectEditWindow() # The object definition window / wizard |
| 87 | + window.open_object_edit_frame(Car, combo, old_data=old) # Open the actual frame |
| 88 | +
|
| 89 | + def print_defined(): |
| 90 | + data = combo.get() |
| 91 | + data = wiz.convert_to_objects(data) # Convert any abstract ObjectInfo objects into actual Python objects |
| 92 | + print(f"Object: {data}; Type: {type(data)}",) # Print the object and it's datatype |
| 93 | +
|
| 94 | +
|
| 95 | + # Main GUI structure |
| 96 | + ttk.Button(text="Define Car", command=make_car).pack() |
| 97 | + ttk.Button(text="Edit Car", command=lambda: make_car(combo.get())).pack() |
| 98 | + ttk.Button(text="Print defined", command=print_defined).pack() |
| 99 | + root.mainloop() |
| 100 | +
|
| 101 | +
|
| 102 | +We can see that the ``Wheel`` is now an abstract class. |
| 103 | +It is then inherited by ``WinterWheel`` and ``SummerWheel``. |
| 104 | +If we try to define the ``wheels`` parameter of our ``Car`` object, only these two inherited classes |
| 105 | +will be definable. |
| 106 | + |
| 107 | +.. image:: ./images/new_define_frame_list_abstractclass.png |
| 108 | + :width: 15cm |
| 109 | + |
| 110 | +We can see that while ``WinterWheel`` and ``SummerWheel`` are definable (due to :ref:`Polymorphism`), |
| 111 | +``Wheel`` is not. |
0 commit comments