|
| 1 | +========================= |
| 2 | +Generic types (classes) |
| 3 | +========================= |
| 4 | + |
| 5 | +A generic type in Python is a class, that inherits the :class:`typing.Generic` class. |
| 6 | +It can be used to define arbitrary type parameters, which can be given a value using a subscription operator ``[]``. |
| 7 | + |
| 8 | +Arbitrary types / type variables can be created using the :class:`typing.TypeVar` class. |
| 9 | + |
| 10 | +.. note:: |
| 11 | + |
| 12 | + Since Python 3.12, the :class:`typing.TypeVar` no longer requires manual definition |
| 13 | + and can be omitted. |
| 14 | + |
| 15 | + |
| 16 | +.. code-block:: python |
| 17 | + :linenos: |
| 18 | +
|
| 19 | + from typing import Generic, TypeVar |
| 20 | +
|
| 21 | +
|
| 22 | + T = TypeVar('T') # From Python 3.12 forward this is optional |
| 23 | +
|
| 24 | + class MyClass(Generic[T]): |
| 25 | + def __init__(self, a: int, b: T): |
| 26 | + ... |
| 27 | +
|
| 28 | +
|
| 29 | + my_instance1: MyClass[float] = MyClass(5, 5.5) |
| 30 | + my_instance2: MyClass[str] = MyClass(5, "Some text") |
| 31 | + my_instance3: MyClass[int] = MyClass(5, 0) |
| 32 | +
|
| 33 | +
|
| 34 | +In the above example, the ``T`` is a type variable. It is also a type parameter inside ``MyClass``. |
| 35 | +When instances are created, the variables are annotated with a type subscription (e. g., ``MyClass[int]``). |
| 36 | + |
| 37 | +While Python itself doesn't care about the types given in the subscription, and just ignores incorrect types, |
| 38 | +TkClassWizard resolves these subscripted types into the ``__init__`` function of a class, allowing the definition |
| 39 | +of arbitrary types with the use of type generics. |
| 40 | + |
| 41 | +If we take the above example and pop it into TkClassWizard with ``MyClass`` having a :class:`float` annotation, |
| 42 | +we would get the following types in the ``New <type>`` options: |
| 43 | + |
| 44 | +.. image:: ./images/new_define_frame_struct_generics.png |
| 45 | + :width: 15cm |
| 46 | + |
| 47 | + |
| 48 | +.. code-block:: python |
| 49 | + :caption: Generic type, given :class:`float` as type parameter |
| 50 | + :linenos: |
| 51 | + :emphasize-lines: 21 |
| 52 | +
|
| 53 | + from typing import Generic, TypeVar |
| 54 | +
|
| 55 | + import tkclasswiz as wiz |
| 56 | + import tkinter as tk |
| 57 | + import tkinter.ttk as ttk |
| 58 | +
|
| 59 | + T = TypeVar('T') # From Python 3.12 forward this is optional |
| 60 | +
|
| 61 | + class MyClass(Generic[T]): |
| 62 | + def __init__(self, a: int, b: T): |
| 63 | + ... |
| 64 | +
|
| 65 | + root = tk.Tk("Test") |
| 66 | +
|
| 67 | + combo = wiz.ComboBoxObjects(root) |
| 68 | + combo.pack(fill=tk.X, padx=5) |
| 69 | +
|
| 70 | + def open(): |
| 71 | + window = wiz.ObjectEditWindow() # The object definition window / wizard |
| 72 | + window.open_object_edit_frame( |
| 73 | + MyClass[float], |
| 74 | + combo |
| 75 | + ) # Open the actual frame |
| 76 | +
|
| 77 | + ttk.Button(text="Define", command=open).pack() |
| 78 | + root.mainloop() |
0 commit comments