From be44243136d710eec0345f8459a64da377bab357 Mon Sep 17 00:00:00 2001 From: Simeon Simeonov Date: Sun, 29 Mar 2026 13:43:38 +0200 Subject: Re-format reveal.js slides and add templates --- notebooks/python/python_oo.ipynb | 151 ++++++++++++++++++++++++++++----------- 1 file changed, 108 insertions(+), 43 deletions(-) (limited to 'notebooks/python/python_oo.ipynb') diff --git a/notebooks/python/python_oo.ipynb b/notebooks/python/python_oo.ipynb index e38d7f3..1ef2a72 100644 --- a/notebooks/python/python_oo.ipynb +++ b/notebooks/python/python_oo.ipynb @@ -66,7 +66,55 @@ "\n", "- improving modularity\n", "\n", - "- providing foundation for a more intuitive design" + "- providing foundation for a more intuitive design\n", + "\n", + "A pseudo code example:\n", + "\n", + "```c\n", + "/* crating and drawing a (pseudo) widget in C */\n", + "my_widget = mylibrary_widget_init(); /* a struct of the type widget */\n", + "mylibrary_widget_draw(my_widget);\n", + "```\n", + "\n", + "```python\n", + "# creating and drawing a (pseudo) widget in Python\n", + "import mylibrary\n", + "\n", + "my_widget = mylibrary.Widget()\n", + "my_widget.draw()\n", + "```" + ] + }, + { + "cell_type": "markdown", + "id": "878cb805-a2d7-46a3-a34a-0507495420f7", + "metadata": {}, + "source": [ + "# Do I need to know / understand OOP when I am programming in Python?\n", + "\n", + "Most definitely - **yes**.\n", + "\n", + "While using programming language that is not object-oriented (like *C* and / or *Rust*) is always possible,\n", + "when using Python there are no *sane* alternatives that have ever been demonstrated:\n", + "\n", + "- Understanding OO behaviour and syntax is a must in order to understand Python code in general\n", + "- OOP is at the core of Python's design - everything in Python is an object\n", + "- Good programming and design is not only about solving a particular task. It is about reusing code and making your code reusable in turn\n", + "- Following the priciples described above is more challenging when not using OO in general." + ] + }, + { + "cell_type": "markdown", + "id": "b83dc815-ed51-4e5a-a30e-fa0252217194", + "metadata": {}, + "source": [ + "# I am doing only simple things with Python. Do I still need to learn OOP?\n", + "\n", + "Most definitely - **yes**.\n", + "\n", + "- A simple task provides a great opportunity to learn and practice OOP\n", + "- Using OOP **does not mean overcomplicating** your code. When applied correctly - it means the opposite\n", + "- Doing \"simple things\" exclusively is not very ambitious approach to programming and learning in general" ] }, { @@ -109,13 +157,20 @@ "\n", "- object - an instance of a class that may contain its own attributes as well as references to its class' attributes\n", "\n", - "- attribute - variable, property, function defined in the class and present in its instances\n", + "- attribute - variable, property, function defined in the class and present in its instances - in Python: any name following a dot. Attributes may be *read-only* or *writable*. In the latter case, assignment to attributes is possible.\n", "\n", "- class variable - attribute of which a single copy exists, regardless of how many instances of the class exist\n", "\n", "- object / instance variable, object / instance attribute - attribute for which each instantiated object of the class has a separate copy, or instance\n", "\n", - "- method - member function - function that is an attribute" + "- method - member function - function that is an attribute\n", + "\n", + "```python\n", + "# my_str_obj is an object (instance) of the class str\n", + "my_str_object = str(\"Test\") # or my_str_object = \"Test\"\n", + "my_str_object.lower # attribute that is a function -> method\n", + "my_str_object.lower() # calling the method \"lower\"\n", + "```" ] }, { @@ -218,7 +273,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 41, "id": "a9c30a5d-aa86-45ab-b897-f051df80f1d8", "metadata": {}, "outputs": [ @@ -226,17 +281,17 @@ "name": "stdout", "output_type": "stream", "text": [ - "car1.get_obj_info_str() = \"I am <__main__.Car object at 0x7f70e643d590> with id 140122876269968 from with id 93964809433504\"\n", - "car2.get_obj_info_str() = \"I am <__main__.Car object at 0x7f70e63ddf10> with id 140122875879184 from with id 93964809433504\"\n", - "car1.model = 'BMW', car1.reg_nr = 'EC76183', car1.extras = ['GPSnav', 'Sound system'], id(car1.cls_extras) = 140122875882432, id(car1.get_obj_info_str) = 140122875878080\n", - "car2.model = 'Scoda', car2.reg_nr = 'BD77655', car2.extras = ['GPSnav'], id(car2.cls_extras) = 140122875882432, id(car2.get_obj_info_str) = 140122875880192\n", - "id(Car.cls_extras) = 140122875882432, id(Car.get_obj_info_str) = 140122875964416\n", - "car1.cls_extras = ['GPSnav', 'Sound system'], id(car1.cls_extras) = 140122875882432\n", - "car2.cls_extras = ['GPSnav', 'Sound system'], id(car2.cls_extras) = 140122875882432\n", + "car1.get_obj_info_str() = \"I am <__main__.Car object at 0x7fd00029ff80> with id 140531332677504 from with id 93887620597168\"\n", + "car2.get_obj_info_str() = \"I am <__main__.Car object at 0x7fd00029ea50> with id 140531332672080 from with id 93887620597168\"\n", + "car1.model = 'BMW', car1.reg_nr = 'EC76183', car1.extras = ['GPSnav', 'Sound system'], id(car1.cls_extras) = 140531337557440, id(car1.get_obj_info_str) = 140531251735488\n", + "car2.model = 'Scoda', car2.reg_nr = 'BD77655', car2.extras = ['GPSnav'], id(car2.cls_extras) = 140531337557440, id(car2.get_obj_info_str) = 140531251728064\n", + "id(Car.cls_extras) = 140531337557440, id(Car.get_obj_info_str) = 140531252010400\n", + "car1.cls_extras = ['GPSnav', 'Sound system'], id(car1.cls_extras) = 140531337557440\n", + "car2.cls_extras = ['GPSnav', 'Sound system'], id(car2.cls_extras) = 140531337557440\n", "True\n", "hasattr(car1, 'import_tax_paid') = True\n", "hasattr(car2, 'import_tax_paid') = False\n", - "id(car1.__class__) = 93964809433504, id(car2.__class__) = 93964809433504, id(Car) = 93964809433504\n" + "id(car1.__class__) = 93887620597168, id(car2.__class__) = 93887620597168, id(Car) = 93887620597168\n" ] } ], @@ -288,7 +343,7 @@ "# - checks if 'attr' is an instance attribute\n", "# - checks if 'attr' is a class attribute (through the method resolution order - MRO)\n", "# - raises AttributeError\n", - "# setter:\n", + "# setter (making the attribute writable):\n", "# - (re)defines an instance attribute\n", "\n", "# Details not covered in this course:\n", @@ -323,7 +378,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 42, "id": "391e816e-c53b-454b-9e9f-3587234f1fea", "metadata": {}, "outputs": [ @@ -413,7 +468,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 43, "id": "e82feefb-025d-4cbb-a8ac-ddc3cc01269c", "metadata": {}, "outputs": [ @@ -495,7 +550,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 44, "id": "71a90ec6-3cc0-441e-a866-c2c2b9984559", "metadata": {}, "outputs": [ @@ -613,7 +668,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 45, "id": "a4d2735d-d167-4f43-ad55-cb3d13ece2dc", "metadata": {}, "outputs": [ @@ -656,7 +711,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 46, "id": "a6ead5e6-e987-4bda-bf48-30f91877278d", "metadata": {}, "outputs": [ @@ -715,7 +770,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 47, "id": "11f6a3fb-64d7-40a9-a130-27548ec4b802", "metadata": {}, "outputs": [ @@ -797,7 +852,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 48, "id": "c345a69f-02da-40e7-bb37-c0511b6af096", "metadata": {}, "outputs": [ @@ -806,6 +861,9 @@ "output_type": "stream", "text": [ "9\n", + "9\n", + "140531251904896\n", + "140531251904896\n", "Vector.from_str('1:1:5:6') = Vector(start=Point(x=1, y=1), end=Point(x=5, y=6))\n" ] } @@ -837,7 +895,14 @@ " Point(coordinates[2], coordinates[3]),\n", " )\n", "\n", - "print(Point.get_manhattan_distance(Point(1, 1), Point(5, 6)))\n", + "point1 = Point(1, 1)\n", + "point2 = Point(5, 6)\n", + "print(f\"{point1.get_manhattan_distance(point1, point2)}\") # works, but should not be used in that way\n", + "print(f\"{Point.get_manhattan_distance(point1, point2)}\")\n", + "print(f\"{id(point1.get_manhattan_distance)}\")\n", + "print(f\"{id(point2.get_manhattan_distance)}\")\n", + "# print(f\"{id(point1.from_str)}\") <-- not available as an instance attribute\n", + "\n", "print(f\"{Vector.from_str('1:1:5:6') = }\")" ] }, @@ -890,7 +955,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 49, "id": "44d47e1c-1a06-4577-8db6-6ec78ce5cef8", "metadata": {}, "outputs": [ @@ -903,32 +968,32 @@ "\n", "class TigerShark(Fish)\n", " | TigerShark(weight: int, alive: bool = True, **kwargs)\n", - " | \n", + " |\n", " | Base class for all tiger sharks\n", - " | \n", + " |\n", " | Method resolution order:\n", " | TigerShark\n", " | Fish\n", " | Animal\n", " | builtins.object\n", - " | \n", + " |\n", " | Methods defined here:\n", - " | \n", + " |\n", " | __init__(self, weight: int, alive: bool = True, **kwargs)\n", " | Initialize self. See help(type(self)) for accurate signature.\n", - " | \n", + " |\n", " | ----------------------------------------------------------------------\n", " | Data descriptors inherited from Animal:\n", - " | \n", + " |\n", " | __dict__\n", - " | dictionary for instance variables (if defined)\n", - " | \n", + " | dictionary for instance variables\n", + " |\n", " | __weakref__\n", - " | list of weak references to the object (if defined)\n", - " | \n", + " | list of weak references to the object\n", + " |\n", " | alive\n", " | getter property alive\n", - " | \n", + " |\n", " | weight\n", " | getter property weight\n", "\n", @@ -1064,7 +1129,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 50, "id": "e6076925-75f0-456c-aed5-7db0a24a67a1", "metadata": {}, "outputs": [ @@ -1169,7 +1234,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 51, "id": "cf79617a-95d2-48f2-be66-fa1fa34e4e04", "metadata": {}, "outputs": [ @@ -1192,14 +1257,14 @@ " | Adam\n", " | Eve\n", " | builtins.object\n", - " | \n", + " |\n", " | Data descriptors inherited from Adam:\n", - " | \n", + " |\n", " | __dict__\n", - " | dictionary for instance variables (if defined)\n", - " | \n", + " | dictionary for instance variables\n", + " |\n", " | __weakref__\n", - " | list of weak references to the object (if defined)\n", + " | list of weak references to the object\n", "\n" ] } @@ -1256,7 +1321,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 52, "id": "95a1d0cc-0006-4ddc-b08d-d0b8b02acfba", "metadata": {}, "outputs": [ @@ -1324,7 +1389,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 53, "id": "38066107-0190-4383-ba14-a4e9cd454a9c", "metadata": {}, "outputs": [ @@ -1418,7 +1483,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.7" + "version": "3.13.9" } }, "nbformat": 4, -- cgit v1.3