{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from abc import ABC, abstractmethod\n", "\n", "\n", "class Component(ABC):\n", "\n", " @abstractmethod\n", " def add_widget(self, widget):\n", " pass\n", "\n", "\n", "class Panel(Component):\n", " def add_widget(self, widget):\n", " # Implementation for adding a widget to a panel\n", " pass\n", "\n", "\n", "class Window(Component):\n", " def add_widget(self, widget):\n", " # Implementation for adding a widget to a window\n", " pass" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "<__main__.Panel at 0x1046cc500>" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Panel()" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "Panel().add_widget(None)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "from abc import ABC, abstractmethod\n", "from typing import Protocol\n", "\n", "\n", "class Component(ABC):\n", "\n", " @abstractmethod\n", " def add_widget(self, widget):\n", " pass\n", "\n", "\n", "class GUIComponent(Protocol):\n", " def add_widget(self, widget) -> None:\n", " ...\n", "\n", "\n", "class Panel(Component):\n", " def add_widget(self, widget):\n", " \"\"\"\n", " Adds a widget to the panel.\n", "\n", " :param widget: The widget to be added.\n", " \"\"\"\n", " pass\n", "\n", "\n", "class Window(Component):\n", " def add_widget(self, widget):\n", " \"\"\"\n", " Adds a widget to the window.\n", "\n", " :param widget: The widget to be added.\n", " \"\"\"\n", " pass\n", "\n", "\n", "def add_to_component(component: GUIComponent, widget):\n", " component.add_widget(widget)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "add_to_component(Window(), Panel())" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "py-kivy-9yq2BF1e-py3.12", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.4" } }, "nbformat": 4, "nbformat_minor": 2 }