Skip to content

Commit 64d72d6

Browse files
committed
Inheritance notebook fixes
1 parent 5642501 commit 64d72d6

File tree

1 file changed

+28
-18
lines changed

1 file changed

+28
-18
lines changed

week8/1_Inheritance.ipynb

+28-18
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,6 @@
147147
"metadata": {},
148148
"outputs": [],
149149
"source": [
150-
"import random\n",
151-
"\n",
152-
"\n",
153150
"class Song:\n",
154151
" \"\"\"Represent a Song in our lyrics site.\n",
155152
"\n",
@@ -159,7 +156,7 @@
159156
" The name of the song.\n",
160157
" lyrics : str\n",
161158
" The lyrics of the song.\n",
162-
" _artists : list of str or str, optional\n",
159+
" artists : list of str or str, optional\n",
163160
" Can be either a list, or a string separated by commas.\n",
164161
"\n",
165162
" Attributes\n",
@@ -1119,7 +1116,7 @@
11191116
"<p style=\"text-align: right; direction: rtl; float: right; clear: both;\">\n",
11201117
" כיוון שהטריק הזה נפוץ יחסית, פייתון נותנת בידינו דרך נוחה להתייחס למחלקת־העל, כשהפרמטר הראשון שנעביר לפעולה שבה הוא <var>self</var>.<br>\n",
11211118
" במקום לכתוב בכל פעם את הביטוי המתיש <code dir=\"ltr\">Song.__init__(self)</code>, נוכל לכתוב <code dir=\"ltr\">super().__init__()</code>.<br>\n",
1122-
" הפונקציה <var>super</var> מאפשרת לנו להתייחס להעברת המופע שיצרנו במחלקת־הבת, לתוך פעולה הנמצאת מחלקת־העל.\n",
1119+
" הפונקציה <var>super</var> מאפשרת לנו להעביר את המופע שיצרנו בתת־המחלקה, לתוך פעולה הנמצאת במחלקת־העל.\n",
11231120
"</p>"
11241121
]
11251122
},
@@ -1174,7 +1171,7 @@
11741171
"metadata": {},
11751172
"source": [
11761173
"<p style=\"text-align: right; direction: rtl; float: right; clear: both;\">\n",
1177-
" כידוע לכם בכפר הדרדרסים יש הרבה דרדרסים \"רגילים\", אבל יש גם כמה דרדסים מפורסמים, כמו דרדסבא, דרדסית ודרדשף.<br>\n",
1174+
" כידוע לכם בכפר הדרדסים יש הרבה דרדסים \"רגילים\", אבל יש גם כמה דרדסים מפורסמים, כמו דרדסבא, דרדסית ודרדשף.<br>\n",
11781175
"</p>"
11791176
]
11801177
},
@@ -1554,7 +1551,7 @@
15541551
"outputs": [],
15551552
"source": [
15561553
"print(Rectangle(5, 6))\n",
1557-
"print(Square(3)"
1554+
"print(Square(3))"
15581555
]
15591556
},
15601557
{
@@ -1631,7 +1628,7 @@
16311628
"metadata": {},
16321629
"source": [
16331630
"<p style=\"text-align: right; direction: rtl; float: right; clear: both;\">\n",
1634-
" לאחר עריכת רוחב הריבוע ציפינו ששטח הריבוע יהיה 9, אך ארכיטקטורת הקוד שלנו כשלה בטיפול במקרה הזה.<br>\n",
1631+
" לאחר עריכת רוחב הריבוע ציפינו ששטח הריבוע יהיה 25, אך ארכיטקטורת הקוד שלנו כשלה בטיפול במקרה הזה.<br>\n",
16351632
" הקוד שלנו שינה רק את הרוחב של הריבוע ולא את אורכו, בזמן שבריבוע דבר כזה לא אמור להתאפשר.<br>\n",
16361633
" בשורה התחתונה – הקוד שלנו נשבר כיוון שלא עקבנו אחרי עקרון ההחלפה.\n",
16371634
"</p>"
@@ -2196,6 +2193,9 @@
21962193
"metadata": {},
21972194
"outputs": [],
21982195
"source": [
2196+
"import datetime\n",
2197+
"\n",
2198+
"\n",
21992199
"class Order:\n",
22002200
" DELIVERY_PRICE_IN_USD = 5\n",
22012201
" VAT_IN_PERCENTS = 20\n",
@@ -2212,9 +2212,14 @@
22122212
" base_price = sum(product.price for product in self.products)\n",
22132213
" return base_price + self._calculate_extra_price(base_price)\n",
22142214
"\n",
2215-
" def _calculate_extra_price(self, base_price, include_vat=True):\n",
2215+
" def _calculate_extra_price(\n",
2216+
" self, base_price, include_vat=True, include_delivery=True,\n",
2217+
" ):\n",
22162218
" tax = self.VAT_IN_PERCENTS / 100\n",
2217-
" return base_price * tax + self.DELIVERY_PRICE_IN_USD\n",
2219+
" price = base_price * tax\n",
2220+
" if include_delivery:\n",
2221+
" price = price + self.DELIVERY_PRICE_IN_USD\n",
2222+
" return price\n",
22182223
"\n",
22192224
" def __str__(self):\n",
22202225
" return (\n",
@@ -2230,8 +2235,8 @@
22302235
"class PhoneOrder(Order):\n",
22312236
" PHONE_CALL_TOLL_IN_USD = 1.99\n",
22322237
"\n",
2233-
" def _calculate_extra_price(self, base_price, include_vat=True):\n",
2234-
" base_price = super()._calculate_extra_price(base_price, include_vat)\n",
2238+
" def _calculate_extra_price(self, base_price, **kwargs):\n",
2239+
" base_price = super()._calculate_extra_price(base_price, **kwargs)\n",
22352240
" return base_price + self.PHONE_CALL_TOLL_IN_USD\n",
22362241
"\n",
22372242
"\n",
@@ -2240,9 +2245,14 @@
22402245
"\n",
22412246
"\n",
22422247
"class StoreOrder(Order):\n",
2243-
" def __init__(self, *args, **kwargs):\n",
2244-
" super().__init__(*args, **kwargs)\n",
2245-
" self.delivered = True"
2248+
" def __init__(self, **kwargs):\n",
2249+
" super().__init__(**kwargs)\n",
2250+
" self.delivered = True\n",
2251+
"\n",
2252+
" def _calculate_extra_price(self, base_price, **kwargs):\n",
2253+
" return super()._calculate_extra_price(\n",
2254+
" base_price, include_delivery=False, **kwargs,\n",
2255+
" )"
22462256
]
22472257
},
22482258
{
@@ -2255,15 +2265,15 @@
22552265
"\n",
22562266
"book1 = Product(1, \"The Fountainhead\", 27.99)\n",
22572267
"book2 = Product(2, \"Thinking, Fast and Slow\", 19.69)\n",
2258-
"order = PhoneOrder(251, 666, [book1, book2])\n",
2268+
"order = PhoneOrder(seller_id=251, buyer_id=666, products=[book1, book2])\n",
22592269
"print(order)\n",
22602270
"\n",
22612271
"print('\\n\\n')\n",
2262-
"order = StoreOrder(251, 666, [book1, book2])\n",
2272+
"order = StoreOrder(seller_id=251, buyer_id=666, products=[book1, book2])\n",
22632273
"print(order)\n",
22642274
"\n",
22652275
"print('\\n\\n')\n",
2266-
"order = OnlineOrder(251, 666, [book1, book2])\n",
2276+
"order = OnlineOrder(seller_id=251, buyer_id=666, products=[book1, book2])\n",
22672277
"print(order)"
22682278
]
22692279
},

0 commit comments

Comments
 (0)