-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
87c72cb
commit a0ca2cc
Showing
1 changed file
with
273 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,273 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"id": "3afa5dd3-247a-4330-98e9-79bf52554c06", | ||
"metadata": {}, | ||
"source": [ | ||
"## You have to implement area()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"id": "77136081-d4fc-4ed4-8175-7a4956b4af0f", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Square area: 25\n", | ||
"Circle area: 28.259999999999998\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"from abc import ABC, abstractmethod\n", | ||
"\n", | ||
"class Shape(ABC): # Abstract base class for shapes\n", | ||
" @abstractmethod\n", | ||
" def area(self):\n", | ||
" pass # Abstract method, subclasses must implement\n", | ||
"\n", | ||
"class Square(Shape): # Concrete subclass of Shape\n", | ||
" def __init__(self, side):\n", | ||
" self.side = side\n", | ||
"\n", | ||
" def area(self):\n", | ||
" return self.side * self.side\n", | ||
"\n", | ||
"class Circle(Shape): # Concrete subclass of Shape\n", | ||
" def __init__(self, radius):\n", | ||
" self.radius = radius\n", | ||
"\n", | ||
" def area(self):\n", | ||
" return 3.14 * self.radius * self.radius\n", | ||
"\n", | ||
"# Trying to create a Shape instance directly will fail\n", | ||
"# since it's an abstract class\n", | ||
"# shape = Shape() # TypeError: Can't instantiate abstract class Shape with abstract methods area\n", | ||
"\n", | ||
"square = Square(5)\n", | ||
"circle = Circle(3)\n", | ||
"\n", | ||
"print(f\"Square area: {square.area()}\")\n", | ||
"print(f\"Circle area: {circle.area()}\")\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "fdad0d0e-0185-4464-9371-7df90159e42f", | ||
"metadata": {}, | ||
"source": [ | ||
"### You can't skip implementation" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"id": "2c5db76b-ed42-47fd-8ee7-c33cc129d826", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Can't instantiate abstract class Triangle with abstract methods area\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"class Triangle(Shape):\n", | ||
" pass\n", | ||
"\n", | ||
"try:\n", | ||
" t = Triangle()\n", | ||
"except TypeError as error:\n", | ||
" print(error)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "48e01ee7-1655-4769-8915-9dc5e328600c", | ||
"metadata": {}, | ||
"source": [ | ||
"### but you can pass it again with pass" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"id": "43457ae6-bf75-49d8-9105-3936ff031fee", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"None\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"class Square(Shape): # Concrete subclass of Shape\n", | ||
" def area(self):\n", | ||
" pass\n", | ||
"\n", | ||
"s = Square()\n", | ||
"print(s.area())" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "740b1c0b-5665-4f72-a1b8-6cf6637c427d", | ||
"metadata": {}, | ||
"source": [ | ||
"### and you can't instantiate the base class" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"id": "5d9d558a-6563-4ba8-8d8d-7442a36482cc", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Can't instantiate abstract class Shape with abstract methods area\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"try:\n", | ||
" s = Shape()\n", | ||
"except TypeError as error:\n", | ||
" print(error)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "d892541c-818b-4639-aeda-61d6de159ee1", | ||
"metadata": {}, | ||
"source": [ | ||
"## And you can register subclasses to be like a virtual base class (Godfather to them)\n", | ||
"### Adobted kids, not DNA kids" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 5, | ||
"id": "3837f4b3-2c40-4a67-a626-7e888900924a", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"float" | ||
] | ||
}, | ||
"execution_count": 5, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"import abc\n", | ||
"class Double(metaclass=abc.ABCMeta):\n", | ||
" \"\"\"Double precision floating point number.\"\"\"\n", | ||
" pass\n", | ||
"\n", | ||
"Double.register(float)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 6, | ||
"id": "b239d976-8b37-4a7b-be9e-7ad11458b061", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"True" | ||
] | ||
}, | ||
"execution_count": 6, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"issubclass(float, Double)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 7, | ||
"id": "5ea12601-9d55-460b-832b-cfb23f5e37b9", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"True" | ||
] | ||
}, | ||
"execution_count": 7, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"isinstance(1.2345, Double)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 8, | ||
"id": "0dc639a4-0c21-448e-9c0f-534e8fc2de6f", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"True\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"@Double.register\n", | ||
"class Double64:\n", | ||
" \"\"\"A 64-bit double-precision floating-point number.\"\"\"\n", | ||
" pass\n", | ||
"\n", | ||
"print(issubclass(Double64, Double)) # True" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3 (ipykernel)", | ||
"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.8.19" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |