|
215 | 215 | },
|
216 | 216 | {
|
217 | 217 | "cell_type": "code",
|
218 |
| - "execution_count": null, |
| 218 | + "execution_count": 3, |
219 | 219 | "metadata": {},
|
220 | 220 | "outputs": [],
|
221 | 221 | "source": [
|
|
249 | 249 | "cell_type": "markdown",
|
250 | 250 | "metadata": {},
|
251 | 251 | "source": [
|
252 |
| - "**[INSERT diagram for class vs object]**" |
| 252 | + "We can also define other \"behaviors\" for each instance of `Person`.\n", |
| 253 | + "Let's define an instance method called `introduce` which introduces a new person.\n", |
| 254 | + "This is done using the same syntax used to define `__init__`. Again, we must take `self` as the first parameter.\n", |
| 255 | + "\n", |
| 256 | + "We form a new string which contains the person's name, age, and profession using the same syntax introduced before and print it.\n", |
| 257 | + "\n", |
| 258 | + "We can then invoke `introduce` by using the `.` syntax and the syntax we used to invoke \"normal\" functions.\n", |
| 259 | + "Note that, while `self` is a required parameter for `introduce`, we don't need to explictly pass it to `introduce`." |
| 260 | + ] |
| 261 | + }, |
| 262 | + { |
| 263 | + "cell_type": "code", |
| 264 | + "execution_count": null, |
| 265 | + "metadata": {}, |
| 266 | + "outputs": [], |
| 267 | + "source": [ |
| 268 | + "class Person:\n", |
| 269 | + " def __init__(self, name, age, profession):\n", |
| 270 | + " self.name = name\n", |
| 271 | + " self.age = age\n", |
| 272 | + " self.profession = profession\n", |
| 273 | + "\n", |
| 274 | + " def introduce(self):\n", |
| 275 | + " print(f\"I'm {self.name}, {self.age} years old. I'm a {self.profession}.\")\n", |
| 276 | + "\n", |
| 277 | + "my_person = Person(\"tomas\", 20, \"computer scientist\")\n", |
| 278 | + "my_person.introduce()" |
| 279 | + ] |
| 280 | + }, |
| 281 | + { |
| 282 | + "cell_type": "markdown", |
| 283 | + "metadata": {}, |
| 284 | + "source": [ |
| 285 | + "> Task 1: Complete the `eat` method which will allow instances of `Person` to eat.\n", |
| 286 | + "\n", |
| 287 | + "`eat` should take the food name as a parameter and return \"\\<Name of the person\\> is eating \\<name of the food\\>\"" |
| 288 | + ] |
| 289 | + }, |
| 290 | + { |
| 291 | + "cell_type": "code", |
| 292 | + "execution_count": null, |
| 293 | + "metadata": {}, |
| 294 | + "outputs": [], |
| 295 | + "source": [ |
| 296 | + "class Person:\n", |
| 297 | + " def __init__(self, name, age, profession):\n", |
| 298 | + " self.name = name\n", |
| 299 | + " self.age = age\n", |
| 300 | + " self.profession = profession\n", |
| 301 | + "\n", |
| 302 | + " def introduce(self):\n", |
| 303 | + " print(f\"I'm {self.name}, {self.age} years old. I'm a {self.profession}.\")\n", |
| 304 | + " \n", |
| 305 | + " def eat(self, food):\n", |
| 306 | + " return \"\"\n", |
| 307 | + "\n", |
| 308 | + "my_person = Person(\"tomas\", 20, \"computer scientist\")\n", |
| 309 | + "my_person.introduce()\n", |
| 310 | + "\n", |
| 311 | + "\n", |
| 312 | + "# DO NOT MODIFY\n", |
| 313 | + "assert my_person.eat(\"pasta\") == \"tomas is eating pasta\", \"Test failed: Something went wrong, expected 'tomas is eating pasta'\"" |
253 | 314 | ]
|
254 | 315 | },
|
255 | 316 | {
|
|
287 | 348 | "tomas.describe()"
|
288 | 349 | ]
|
289 | 350 | },
|
| 351 | + { |
| 352 | + "cell_type": "markdown", |
| 353 | + "metadata": {}, |
| 354 | + "source": [ |
| 355 | + "**[INSERT diagram for class vs object]**" |
| 356 | + ] |
| 357 | + }, |
290 | 358 | {
|
291 | 359 | "cell_type": "markdown",
|
292 | 360 | "metadata": {},
|
|
0 commit comments