|
116 | 116 | "metadata": {}
|
117 | 117 | },
|
118 | 118 | {
|
119 |
| - "cell_type": "code", |
120 |
| - "execution_count": 2, |
| 119 | + "cell_type": "markdown", |
121 | 120 | "source": [
|
122 | 121 | "## `Developer` class"
|
123 | 122 | ],
|
124 |
| - "outputs": [], |
125 | 123 | "metadata": {}
|
126 | 124 | },
|
127 | 125 | {
|
|
162 | 160 | },
|
163 | 161 | {
|
164 | 162 | "cell_type": "code",
|
165 |
| - "execution_count": null, |
166 |
| - "source": [], |
167 |
| - "outputs": [], |
| 163 | + "execution_count": 7, |
| 164 | + "source": [ |
| 165 | + "# method resolution order\n", |
| 166 | + "help(Developer)" |
| 167 | + ], |
| 168 | + "outputs": [ |
| 169 | + { |
| 170 | + "output_type": "stream", |
| 171 | + "name": "stdout", |
| 172 | + "text": [ |
| 173 | + "Help on class Developer in module __main__:\n", |
| 174 | + "\n", |
| 175 | + "class Developer(Employee)\n", |
| 176 | + " | Developer(name, surname)\n", |
| 177 | + " | \n", |
| 178 | + " | Method resolution order:\n", |
| 179 | + " | Developer\n", |
| 180 | + " | Employee\n", |
| 181 | + " | builtins.object\n", |
| 182 | + " | \n", |
| 183 | + " | Methods inherited from Employee:\n", |
| 184 | + " | \n", |
| 185 | + " | __init__(self, name, surname)\n", |
| 186 | + " | Initialize self. See help(type(self)) for accurate signature.\n", |
| 187 | + " | \n", |
| 188 | + " | add_daily_hours(self, daily_hours)\n", |
| 189 | + " | \n", |
| 190 | + " | fullname(self)\n", |
| 191 | + " | \n", |
| 192 | + " | salary(self)\n", |
| 193 | + " | \n", |
| 194 | + " | ----------------------------------------------------------------------\n", |
| 195 | + " | Class methods inherited from Employee:\n", |
| 196 | + " | \n", |
| 197 | + " | from_string(name_str) from builtins.type\n", |
| 198 | + " | \n", |
| 199 | + " | set_hourly_wage(hourly_wage) from builtins.type\n", |
| 200 | + " | \n", |
| 201 | + " | ----------------------------------------------------------------------\n", |
| 202 | + " | Static methods inherited from Employee:\n", |
| 203 | + " | \n", |
| 204 | + " | is_workday(day)\n", |
| 205 | + " | \n", |
| 206 | + " | ----------------------------------------------------------------------\n", |
| 207 | + " | Data descriptors inherited from Employee:\n", |
| 208 | + " | \n", |
| 209 | + " | __dict__\n", |
| 210 | + " | dictionary for instance variables (if defined)\n", |
| 211 | + " | \n", |
| 212 | + " | __weakref__\n", |
| 213 | + " | list of weak references to the object (if defined)\n", |
| 214 | + " | \n", |
| 215 | + " | ----------------------------------------------------------------------\n", |
| 216 | + " | Data and other attributes inherited from Employee:\n", |
| 217 | + " | \n", |
| 218 | + " | count = 2\n", |
| 219 | + " | \n", |
| 220 | + " | hourly_wage = 20\n", |
| 221 | + "\n" |
| 222 | + ] |
| 223 | + } |
| 224 | + ], |
| 225 | + "metadata": {} |
| 226 | + }, |
| 227 | + { |
| 228 | + "cell_type": "markdown", |
| 229 | + "source": [ |
| 230 | + "### Customizing the sublass" |
| 231 | + ], |
168 | 232 | "metadata": {}
|
169 | 233 | },
|
170 | 234 | {
|
171 | 235 | "cell_type": "code",
|
172 |
| - "execution_count": null, |
173 |
| - "source": [], |
| 236 | + "execution_count": 8, |
| 237 | + "source": [ |
| 238 | + "class Developer(Employee):\n", |
| 239 | + " hourly_wage = 30" |
| 240 | + ], |
174 | 241 | "outputs": [],
|
175 | 242 | "metadata": {}
|
176 | 243 | },
|
177 | 244 | {
|
178 | 245 | "cell_type": "code",
|
179 |
| - "execution_count": null, |
180 |
| - "source": [], |
| 246 | + "execution_count": 9, |
| 247 | + "source": [ |
| 248 | + "developer1 = Developer(\"John\", \"Smith\")\n", |
| 249 | + "developer1.add_daily_hours(8)\n", |
| 250 | + "developer1.salary()" |
| 251 | + ], |
| 252 | + "outputs": [ |
| 253 | + { |
| 254 | + "output_type": "execute_result", |
| 255 | + "data": { |
| 256 | + "text/plain": [ |
| 257 | + "240" |
| 258 | + ] |
| 259 | + }, |
| 260 | + "metadata": {}, |
| 261 | + "execution_count": 9 |
| 262 | + } |
| 263 | + ], |
| 264 | + "metadata": {} |
| 265 | + }, |
| 266 | + { |
| 267 | + "cell_type": "markdown", |
| 268 | + "source": [ |
| 269 | + "### Adding attributes to the subclass" |
| 270 | + ], |
| 271 | + "metadata": {} |
| 272 | + }, |
| 273 | + { |
| 274 | + "cell_type": "code", |
| 275 | + "execution_count": 21, |
| 276 | + "source": [ |
| 277 | + "class Developer(Employee):\n", |
| 278 | + " hourly_wage = 30\n", |
| 279 | + "\n", |
| 280 | + " def __init__(self, name, surname, programming_language):\n", |
| 281 | + " super().__init__(name, surname) # using super class constructor\n", |
| 282 | + " self.programming_languages = programming_language" |
| 283 | + ], |
181 | 284 | "outputs": [],
|
182 | 285 | "metadata": {}
|
183 | 286 | },
|
184 | 287 | {
|
185 | 288 | "cell_type": "code",
|
186 |
| - "execution_count": null, |
187 |
| - "source": [], |
| 289 | + "execution_count": 20, |
| 290 | + "source": [ |
| 291 | + "# create objects\n", |
| 292 | + "developer1 = Developer(\"John\", \"Smith\", \"Python\")\n", |
| 293 | + "developer2 = Developer(\"David\", \"Jhonson\", \"Java\")\n", |
| 294 | + "\n", |
| 295 | + "# use objects\n", |
| 296 | + "print(developer1.email)\n", |
| 297 | + "print(developer2.email)\n" |
| 298 | + ], |
| 299 | + "outputs": [ |
| 300 | + { |
| 301 | + "output_type": "stream", |
| 302 | + "name": "stdout", |
| 303 | + "text": [ |
| 304 | + "{'name': 'John', 'surname': 'Smith', 'email': '[email protected]', 'hours': 0, 'id': '00014'}\n", |
| 305 | + "{'name': 'David', 'surname': 'Jhonson', 'email': '[email protected]', 'hours': 0, 'id': '00015'}\n", |
| 306 | + |
| 307 | + |
| 308 | + ] |
| 309 | + } |
| 310 | + ], |
| 311 | + "metadata": {} |
| 312 | + }, |
| 313 | + { |
| 314 | + "cell_type": "markdown", |
| 315 | + "source": [ |
| 316 | + "## `Manager` class" |
| 317 | + ], |
| 318 | + "metadata": {} |
| 319 | + }, |
| 320 | + { |
| 321 | + "cell_type": "code", |
| 322 | + "execution_count": 22, |
| 323 | + "source": [ |
| 324 | + "class Manager(Employee):\n", |
| 325 | + " hourly_wage = 50\n", |
| 326 | + "\n", |
| 327 | + " def __init__(self, name, surname, employees=None):\n", |
| 328 | + " super().__init__(name, surname)\n", |
| 329 | + "\n", |
| 330 | + " self.employees = [] if employees is None else employees" |
| 331 | + ], |
188 | 332 | "outputs": [],
|
189 | 333 | "metadata": {}
|
190 | 334 | },
|
| 335 | + { |
| 336 | + "cell_type": "markdown", |
| 337 | + "source": [ |
| 338 | + "### Good practice\n", |
| 339 | + "- Don't use mutable objects like `list` and `dict` as default values.\n", |
| 340 | + "- That's why here we have used `None` instead of an empty list (`[]`)." |
| 341 | + ], |
| 342 | + "metadata": {} |
| 343 | + }, |
191 | 344 | {
|
192 | 345 | "cell_type": "code",
|
193 |
| - "execution_count": null, |
194 |
| - "source": [], |
| 346 | + "execution_count": 28, |
| 347 | + "source": [ |
| 348 | + "class Manager(Employee):\n", |
| 349 | + " hourly_wage = 50\n", |
| 350 | + "\n", |
| 351 | + " def __init__(self, name, surname, employees=None):\n", |
| 352 | + " super().__init__(name, surname)\n", |
| 353 | + "\n", |
| 354 | + " self.employees = [] if employees is None else employees\n", |
| 355 | + "\n", |
| 356 | + " def add_employee(self, employee):\n", |
| 357 | + " if employee not in self.employees:\n", |
| 358 | + " self.employees.append(employee)\n", |
| 359 | + "\n", |
| 360 | + " def remove_employee(self, employee):\n", |
| 361 | + " if employee in self.employees:\n", |
| 362 | + " self.employees.remove(employee)\n", |
| 363 | + "\n", |
| 364 | + " def print_employees(self):\n", |
| 365 | + " print(f\"{self.fullname()} manages ({', '.join([e.fullname() for e in self.employees])})\")\n" |
| 366 | + ], |
195 | 367 | "outputs": [],
|
196 | 368 | "metadata": {}
|
197 | 369 | },
|
198 | 370 | {
|
199 | 371 | "cell_type": "code",
|
200 |
| - "execution_count": null, |
201 |
| - "source": [], |
| 372 | + "execution_count": 31, |
| 373 | + "source": [ |
| 374 | + "# create object\n", |
| 375 | + "manager1 = Manager(\"Mat\", \"Anderson\", [developer1])\n", |
| 376 | + "\n", |
| 377 | + "# use object\n", |
| 378 | + "manager1.add_employee(developer2)\n", |
| 379 | + "\n", |
| 380 | + "print(manager1.email)\n", |
| 381 | + "manager1.print_employees()" |
| 382 | + ], |
| 383 | + "outputs": [ |
| 384 | + { |
| 385 | + "output_type": "stream", |
| 386 | + "name": "stdout", |
| 387 | + "text": [ |
| 388 | + |
| 389 | + "Mat Anderson manages (John Smith, David Jhonson)\n" |
| 390 | + ] |
| 391 | + } |
| 392 | + ], |
| 393 | + "metadata": {} |
| 394 | + }, |
| 395 | + { |
| 396 | + "cell_type": "code", |
| 397 | + "execution_count": 32, |
| 398 | + "source": [ |
| 399 | + "manager1.remove_employee(developer1)\n", |
| 400 | + "manager1.print_employees()" |
| 401 | + ], |
| 402 | + "outputs": [ |
| 403 | + { |
| 404 | + "output_type": "stream", |
| 405 | + "name": "stdout", |
| 406 | + "text": [ |
| 407 | + "Mat Anderson manages (David Jhonson)\n" |
| 408 | + ] |
| 409 | + } |
| 410 | + ], |
| 411 | + "metadata": {} |
| 412 | + }, |
| 413 | + { |
| 414 | + "cell_type": "markdown", |
| 415 | + "source": [ |
| 416 | + "## `isinstance()` and `issubclass()`\n", |
| 417 | + "- `isinstance()` will tell us wether or not an object is instance of a class.\n", |
| 418 | + "- `issubclass()` will tell us wether or not an object is subclass of another class." |
| 419 | + ], |
| 420 | + "metadata": {} |
| 421 | + }, |
| 422 | + { |
| 423 | + "cell_type": "code", |
| 424 | + "execution_count": 34, |
| 425 | + "source": [ |
| 426 | + "print(isinstance(manager1, Manager))\n", |
| 427 | + "print(isinstance(manager1, Employee))\n", |
| 428 | + "print(isinstance(manager1, Developer))" |
| 429 | + ], |
| 430 | + "outputs": [ |
| 431 | + { |
| 432 | + "output_type": "stream", |
| 433 | + "name": "stdout", |
| 434 | + "text": [ |
| 435 | + "True\n", |
| 436 | + "True\n", |
| 437 | + "False\n" |
| 438 | + ] |
| 439 | + } |
| 440 | + ], |
| 441 | + "metadata": {} |
| 442 | + }, |
| 443 | + { |
| 444 | + "cell_type": "code", |
| 445 | + "execution_count": 37, |
| 446 | + "source": [ |
| 447 | + "print(issubclass(Manager, Employee))\n", |
| 448 | + "print(issubclass(Developer, Employee))" |
| 449 | + ], |
| 450 | + "outputs": [ |
| 451 | + { |
| 452 | + "output_type": "stream", |
| 453 | + "name": "stdout", |
| 454 | + "text": [ |
| 455 | + "True\n", |
| 456 | + "True\n" |
| 457 | + ] |
| 458 | + } |
| 459 | + ], |
| 460 | + "metadata": {} |
| 461 | + }, |
| 462 | + { |
| 463 | + "cell_type": "code", |
| 464 | + "execution_count": 42, |
| 465 | + "source": [ |
| 466 | + "class Manager(Employee):\n", |
| 467 | + " hourly_wage = 50\n", |
| 468 | + "\n", |
| 469 | + " def __init__(self, name, surname, employees=None):\n", |
| 470 | + " super().__init__(name, surname)\n", |
| 471 | + "\n", |
| 472 | + " if employees is None:\n", |
| 473 | + " self.employees = [] \n", |
| 474 | + " else:\n", |
| 475 | + " if isinstance(employees, list):\n", |
| 476 | + " self.employees = employees\n", |
| 477 | + " elif isinstance(employees, Employee):\n", |
| 478 | + " self.employees = [employees]\n", |
| 479 | + " else:\n", |
| 480 | + " self.employees = []\n", |
| 481 | + " print(\"Error! You can only use Employee objects!\")\n", |
| 482 | + "\n", |
| 483 | + " def add_employee(self, employee):\n", |
| 484 | + " if employee not in self.employees:\n", |
| 485 | + " self.employees.append(employee)\n", |
| 486 | + "\n", |
| 487 | + " def remove_employee(self, employee):\n", |
| 488 | + " if employee in self.employees:\n", |
| 489 | + " self.employees.remove(employee)\n", |
| 490 | + "\n", |
| 491 | + " def print_employees(self):\n", |
| 492 | + " print(f\"{self.fullname()} manages ({', '.join([e.fullname() for e in self.employees])})\")\n" |
| 493 | + ], |
202 | 494 | "outputs": [],
|
203 | 495 | "metadata": {}
|
| 496 | + }, |
| 497 | + { |
| 498 | + "cell_type": "code", |
| 499 | + "execution_count": 44, |
| 500 | + "source": [ |
| 501 | + "# create object\n", |
| 502 | + "manager1 = Manager(\"Mat\", \"Anderson\", developer1)\n", |
| 503 | + "\n", |
| 504 | + "# use object\n", |
| 505 | + "manager1.add_employee(developer2)\n", |
| 506 | + "\n", |
| 507 | + "print(manager1.email)\n", |
| 508 | + "manager1.print_employees()" |
| 509 | + ], |
| 510 | + "outputs": [ |
| 511 | + { |
| 512 | + "output_type": "stream", |
| 513 | + "name": "stdout", |
| 514 | + "text": [ |
| 515 | + |
| 516 | + "Mat Anderson manages (John Smith, David Jhonson)\n" |
| 517 | + ] |
| 518 | + } |
| 519 | + ], |
| 520 | + "metadata": {} |
| 521 | + }, |
| 522 | + { |
| 523 | + "cell_type": "markdown", |
| 524 | + "source": [ |
| 525 | + "## Next\n", |
| 526 | + "- Using Python special methods and overloading" |
| 527 | + ], |
| 528 | + "metadata": {} |
204 | 529 | }
|
205 | 530 | ],
|
206 | 531 | "metadata": {
|
|
0 commit comments