|
317 | 317 | "source": [
|
318 | 318 | "We can now represent a person with attributes name, age, and profession. We can introduce a person, and a person can eat.\n",
|
319 | 319 | "This brings us to one of the core principles associated with OOP, **encapsulation**: the object's methods and attributes\n",
|
320 |
| - "are bundled as a single-unit. We also **abstract** away the implementation details of each method. As a programmer you can invoke `eat`\n", |
| 320 | + "are bundled as a single-unit.\n", |
| 321 | + "\n", |
| 322 | + "As a further example, for **encapsulation**, our class can have other attributes that are not exposed to the \"outside world\".\n", |
| 323 | + "For example, we can keep track of the energy of a Person using `_energy` (note the underscore, this represents a private attribute, that is, it shouldn't be used anywhere outside the class), and we use this to decide whether or not a person can walk. The key takeaway for encapsulation is that we only\n", |
| 324 | + "expose what is absolutely necessary to the \"outside world\"." |
| 325 | + ] |
| 326 | + }, |
| 327 | + { |
| 328 | + "cell_type": "code", |
| 329 | + "execution_count": 2, |
| 330 | + "metadata": {}, |
| 331 | + "outputs": [ |
| 332 | + { |
| 333 | + "name": "stdout", |
| 334 | + "output_type": "stream", |
| 335 | + "text": [ |
| 336 | + "Not enough energy!\n", |
| 337 | + "Walking...\n" |
| 338 | + ] |
| 339 | + } |
| 340 | + ], |
| 341 | + "source": [ |
| 342 | + "class Person:\n", |
| 343 | + " def __init__(self, name, age, profession):\n", |
| 344 | + " self.name = name\n", |
| 345 | + " self.age = age\n", |
| 346 | + " self.profession = profession\n", |
| 347 | + " self._energy = 0\n", |
| 348 | + "\n", |
| 349 | + " def introduce(self):\n", |
| 350 | + " print(f\"I'm {self.name}, {self.age} years old. I'm a {self.profession}.\")\n", |
| 351 | + " \n", |
| 352 | + " def eat(self, food):\n", |
| 353 | + " self._energy += 9\n", |
| 354 | + " return \"\"\n", |
| 355 | + "\n", |
| 356 | + " def walk(self):\n", |
| 357 | + " if self._energy < 10:\n", |
| 358 | + " print(\"Not enough energy!\")\n", |
| 359 | + " else:\n", |
| 360 | + " print(\"Walking...\")\n", |
| 361 | + "\n", |
| 362 | + "my_person = Person(\"donald knuth\", 84, \"computer scientist\")\n", |
| 363 | + "my_person.eat(\"food\")\n", |
| 364 | + "my_person.walk()\n", |
| 365 | + "my_person.eat(\"food\")\n", |
| 366 | + "my_person.walk()" |
| 367 | + ] |
| 368 | + }, |
| 369 | + { |
| 370 | + "cell_type": "markdown", |
| 371 | + "metadata": {}, |
| 372 | + "source": [ |
| 373 | + "We also **abstracted** the implementation details of each method. As a programmer you can invoke `eat`\n", |
321 | 374 | "or `introduce` without having to worry about what's going on in the background."
|
322 | 375 | ]
|
323 | 376 | },
|
|
463 | 516 | "the behaviour of the method, in which case you don't need to invoke the method from the parent class."
|
464 | 517 | ]
|
465 | 518 | },
|
| 519 | + { |
| 520 | + "cell_type": "markdown", |
| 521 | + "metadata": {}, |
| 522 | + "source": [] |
| 523 | + }, |
466 | 524 | {
|
467 | 525 | "cell_type": "markdown",
|
468 | 526 | "metadata": {},
|
|
0 commit comments