|
147 | 147 | "metadata": {},
|
148 | 148 | "outputs": [],
|
149 | 149 | "source": [
|
150 |
| - "import random\n", |
151 |
| - "\n", |
152 |
| - "\n", |
153 | 150 | "class Song:\n",
|
154 | 151 | " \"\"\"Represent a Song in our lyrics site.\n",
|
155 | 152 | "\n",
|
|
159 | 156 | " The name of the song.\n",
|
160 | 157 | " lyrics : str\n",
|
161 | 158 | " The lyrics of the song.\n",
|
162 |
| - " _artists : list of str or str, optional\n", |
| 159 | + " artists : list of str or str, optional\n", |
163 | 160 | " Can be either a list, or a string separated by commas.\n",
|
164 | 161 | "\n",
|
165 | 162 | " Attributes\n",
|
|
1119 | 1116 | "<p style=\"text-align: right; direction: rtl; float: right; clear: both;\">\n",
|
1120 | 1117 | " כיוון שהטריק הזה נפוץ יחסית, פייתון נותנת בידינו דרך נוחה להתייחס למחלקת־העל, כשהפרמטר הראשון שנעביר לפעולה שבה הוא <var>self</var>.<br>\n",
|
1121 | 1118 | " במקום לכתוב בכל פעם את הביטוי המתיש <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", |
1123 | 1120 | "</p>"
|
1124 | 1121 | ]
|
1125 | 1122 | },
|
|
1174 | 1171 | "metadata": {},
|
1175 | 1172 | "source": [
|
1176 | 1173 | "<p style=\"text-align: right; direction: rtl; float: right; clear: both;\">\n",
|
1177 |
| - " כידוע לכם בכפר הדרדרסים יש הרבה דרדרסים \"רגילים\", אבל יש גם כמה דרדסים מפורסמים, כמו דרדסבא, דרדסית ודרדשף.<br>\n", |
| 1174 | + " כידוע לכם בכפר הדרדסים יש הרבה דרדסים \"רגילים\", אבל יש גם כמה דרדסים מפורסמים, כמו דרדסבא, דרדסית ודרדשף.<br>\n", |
1178 | 1175 | "</p>"
|
1179 | 1176 | ]
|
1180 | 1177 | },
|
|
1554 | 1551 | "outputs": [],
|
1555 | 1552 | "source": [
|
1556 | 1553 | "print(Rectangle(5, 6))\n",
|
1557 |
| - "print(Square(3)" |
| 1554 | + "print(Square(3))" |
1558 | 1555 | ]
|
1559 | 1556 | },
|
1560 | 1557 | {
|
|
1631 | 1628 | "metadata": {},
|
1632 | 1629 | "source": [
|
1633 | 1630 | "<p style=\"text-align: right; direction: rtl; float: right; clear: both;\">\n",
|
1634 |
| - " לאחר עריכת רוחב הריבוע ציפינו ששטח הריבוע יהיה 9, אך ארכיטקטורת הקוד שלנו כשלה בטיפול במקרה הזה.<br>\n", |
| 1631 | + " לאחר עריכת רוחב הריבוע ציפינו ששטח הריבוע יהיה 25, אך ארכיטקטורת הקוד שלנו כשלה בטיפול במקרה הזה.<br>\n", |
1635 | 1632 | " הקוד שלנו שינה רק את הרוחב של הריבוע ולא את אורכו, בזמן שבריבוע דבר כזה לא אמור להתאפשר.<br>\n",
|
1636 | 1633 | " בשורה התחתונה – הקוד שלנו נשבר כיוון שלא עקבנו אחרי עקרון ההחלפה.\n",
|
1637 | 1634 | "</p>"
|
|
2196 | 2193 | "metadata": {},
|
2197 | 2194 | "outputs": [],
|
2198 | 2195 | "source": [
|
| 2196 | + "import datetime\n", |
| 2197 | + "\n", |
| 2198 | + "\n", |
2199 | 2199 | "class Order:\n",
|
2200 | 2200 | " DELIVERY_PRICE_IN_USD = 5\n",
|
2201 | 2201 | " VAT_IN_PERCENTS = 20\n",
|
|
2212 | 2212 | " base_price = sum(product.price for product in self.products)\n",
|
2213 | 2213 | " return base_price + self._calculate_extra_price(base_price)\n",
|
2214 | 2214 | "\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", |
2216 | 2218 | " 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", |
2218 | 2223 | "\n",
|
2219 | 2224 | " def __str__(self):\n",
|
2220 | 2225 | " return (\n",
|
|
2230 | 2235 | "class PhoneOrder(Order):\n",
|
2231 | 2236 | " PHONE_CALL_TOLL_IN_USD = 1.99\n",
|
2232 | 2237 | "\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", |
2235 | 2240 | " return base_price + self.PHONE_CALL_TOLL_IN_USD\n",
|
2236 | 2241 | "\n",
|
2237 | 2242 | "\n",
|
|
2240 | 2245 | "\n",
|
2241 | 2246 | "\n",
|
2242 | 2247 | "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 | + " )" |
2246 | 2256 | ]
|
2247 | 2257 | },
|
2248 | 2258 | {
|
|
2255 | 2265 | "\n",
|
2256 | 2266 | "book1 = Product(1, \"The Fountainhead\", 27.99)\n",
|
2257 | 2267 | "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", |
2259 | 2269 | "print(order)\n",
|
2260 | 2270 | "\n",
|
2261 | 2271 | "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", |
2263 | 2273 | "print(order)\n",
|
2264 | 2274 | "\n",
|
2265 | 2275 | "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", |
2267 | 2277 | "print(order)"
|
2268 | 2278 | ]
|
2269 | 2279 | },
|
|
0 commit comments