|
215 | 215 | "\n", |
216 | 216 | "Due to the peculiarities of Python, the `self` parameter is required in every method of a class, and is always the first parameter. However, when you call an object's method, you do not need to pass in the `self` parameter, as Python will do this automatically.\n", |
217 | 217 | "\n", |
218 | | - "### special class methods\n", |
| 218 | + "### special or \"magic\" class methods\n", |
219 | 219 | "\n", |
220 | | - "There are a few other special class methods that we can use to do things for us automatically. All of these special methods are denoted by a double underscore in their method name, like `__init__`. \n" |
| 220 | + "There are a few other special class methods that we can use to do things for us automatically. Some people call these _magic_ methods, but there's no magic behind these, just 1s and 0s. if you look deeper all of computers is just funny electrons jumping around on different excitement states on metal and plastic. So much of my life is dedicated to funny electrons...If you think about it, internet influencers and crypto moguls make a living off making electrons move slightly differently on someone's magic handheld box to display images and sound...Kind of a sad life...OKAY IM GOING TO STOP BEING EXISTENTIAL THIS IS JUST A PROGRAMMING CLUB LESSON.\n", |
| 221 | + "guys i think writing these at 9pm was bad decision i woke up at 6am today and had to drive and volunteer and ugh im so stressed out rn its the summer and somehow im swamped in work\n", |
| 222 | + "ANYWAYS, All of these special methods are denoted by a double underscore in their method name, like `__init__`.\n", |
| 223 | + "\n", |
| 224 | + "- `__str__` : this method is called when you call the `str()` function on an object, or when you print an object. It should return a string representation of the object. This is useful for debugging and logging, as it allows you to see the object's properties in a readable format. This is similar to the `toString()` method that you'll `@Override` in Java.\n", |
| 225 | + "- theres actually a whole bunch more than i thought there were, i only knew `__init__` and `__str__` before looking it up lol. theres a whole bunch more that define behavior when you use add, divide, multiply, and other operators on objects. you can check out the full list [here](https://docs.python.org/3/reference/datamodel.html#special-method-names) and [here](https://docs.python.org/3/reference/datamodel.html#emulating-numeric-types) if you're interested.\n" |
221 | 226 | ] |
222 | 227 | }, |
| 228 | + { |
| 229 | + "metadata": {}, |
| 230 | + "cell_type": "code", |
| 231 | + "outputs": [], |
| 232 | + "execution_count": null, |
| 233 | + "source": [ |
| 234 | + "class Apple:\n", |
| 235 | + " def __init__(self, variety, color, weight):\n", |
| 236 | + " self.variety = variety\n", |
| 237 | + " self.color = color\n", |
| 238 | + " self.weight = weight\n", |
| 239 | + " def __str__(self):\n", |
| 240 | + " return f\"{self.color} {self.variety} apple weighing {self.weight} grams\"\n", |
| 241 | + "my_apple = Apple(\"Granny Smith\", \"Green\", 150)\n", |
| 242 | + "print(my_apple) # this will call the __str__ method" |
| 243 | + ], |
| 244 | + "id": "fba9dcd9945e5d69" |
| 245 | + }, |
223 | 246 | { |
224 | 247 | "cell_type": "markdown", |
225 | 248 | "id": "83e689a7", |
226 | 249 | "metadata": {}, |
227 | 250 | "source": [ |
228 | 251 | "## exercise - writing your own Car class\n", |
229 | 252 | "\n", |
230 | | - "yeah yeah you might say, making a car class is soooooo basic. but hey, it's a tried and true method of learning how to use classes. \n", |
| 253 | + "yeah yeah you might say, making a car class is soooooo basic. but hey, it's a tried and true method of learning how to use classes. c.f. my ap comp sci class\n", |
| 254 | + "\n", |
| 255 | + "-- Specifications --\n", |
231 | 256 | "\n", |
232 | | - "For your car, it's going to want to have a few properties. We're going to want to describe the `make` (brand, e.g. Honda), `model` (e.g. Corolla), `year`, `color`, and `mpg` (miles per gallon). These should all go in the constructor. Make sure to initialize an extra property called `gallons`, which is the gallons left in tank, to zero. " |
| 257 | + "For your car, it's going to want to have a few properties. We're going to want to describe the `make` (brand, e.g. Honda), `model` (e.g. Corolla), `year`, `color`, and `mpg` (miles per gallon). These should all go in the constructor. Make sure to initialize an extra property called `gallons`, which is the gallons left in tank, to zero.\n", |
| 258 | + "\n", |
| 259 | + "Make sure to add a `__str__` method that returns a string representation of the car (bonus points for ASCII art!!) which includes all the information about the car. Also, add a method called `add_fuel` that takes in a number of gallons and adds it to the tank, a method called `repaint` which changes the color, and, a method called `check_tank` that prints out how many gallons are left in the tank. Finally, add a method called `drive` that takes in a number of miles and reduces the gallons in the tank based on the mpg. If there is not enough fuel to drive the requested number of miles, drive as far as possible and set the gallons to zero. Then, call a tow truck (just kidding!). The method should return the number of miles driven.\n" |
233 | 260 | ] |
234 | 261 | }, |
235 | 262 | { |
|
238 | 265 | "id": "d4331927360a7115", |
239 | 266 | "metadata": {}, |
240 | 267 | "outputs": [], |
241 | | - "source": [] |
| 268 | + "source": [ |
| 269 | + "# write your code for the car class here VVV\n", |
| 270 | + "#\n", |
| 271 | + "#\n", |
| 272 | + "#\n", |
| 273 | + "#\n", |
| 274 | + "# vroom vroom\n", |
| 275 | + "# ______\n", |
| 276 | + "# /|_||_\\`.__\n", |
| 277 | + "#( _ _ _\\\n", |
| 278 | + "#=`-(_)--(_)-'\n", |
| 279 | + "# man do you remember the cars movies ...that was so long ago...peak cinema...\n", |
| 280 | + "# i feel old now\n", |
| 281 | + "# lightning mcqueen was such a mood tho ngl...i used to play with lightning mcqueen toys all the time in my childhood" |
| 282 | + ] |
242 | 283 | } |
243 | 284 | ], |
244 | 285 | "metadata": { |
|
0 commit comments