|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 1, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [], |
| 8 | + "source": [ |
| 9 | + "class Thing:\n", |
| 10 | + " pass" |
| 11 | + ] |
| 12 | + }, |
| 13 | + { |
| 14 | + "cell_type": "code", |
| 15 | + "execution_count": 3, |
| 16 | + "metadata": {}, |
| 17 | + "outputs": [ |
| 18 | + { |
| 19 | + "name": "stdout", |
| 20 | + "output_type": "stream", |
| 21 | + "text": [ |
| 22 | + "<class '__main__.Thing'>\n" |
| 23 | + ] |
| 24 | + } |
| 25 | + ], |
| 26 | + "source": [ |
| 27 | + "print(Thing)" |
| 28 | + ] |
| 29 | + }, |
| 30 | + { |
| 31 | + "cell_type": "code", |
| 32 | + "execution_count": 4, |
| 33 | + "metadata": {}, |
| 34 | + "outputs": [ |
| 35 | + { |
| 36 | + "data": { |
| 37 | + "text/plain": [ |
| 38 | + "<__main__.Thing at 0x28ca0fbec40>" |
| 39 | + ] |
| 40 | + }, |
| 41 | + "execution_count": 4, |
| 42 | + "metadata": {}, |
| 43 | + "output_type": "execute_result" |
| 44 | + } |
| 45 | + ], |
| 46 | + "source": [ |
| 47 | + "## object of class is example\n", |
| 48 | + "example=Thing()\n", |
| 49 | + "example" |
| 50 | + ] |
| 51 | + }, |
| 52 | + { |
| 53 | + "cell_type": "code", |
| 54 | + "execution_count": 5, |
| 55 | + "metadata": {}, |
| 56 | + "outputs": [ |
| 57 | + { |
| 58 | + "name": "stdout", |
| 59 | + "output_type": "stream", |
| 60 | + "text": [ |
| 61 | + "abc\n" |
| 62 | + ] |
| 63 | + } |
| 64 | + ], |
| 65 | + "source": [ |
| 66 | + "class Thing2:\n", |
| 67 | + " letters = 'abc'\n", |
| 68 | + "print(Thing2.letters)" |
| 69 | + ] |
| 70 | + }, |
| 71 | + { |
| 72 | + "cell_type": "code", |
| 73 | + "execution_count": 9, |
| 74 | + "metadata": {}, |
| 75 | + "outputs": [ |
| 76 | + { |
| 77 | + "name": "stdout", |
| 78 | + "output_type": "stream", |
| 79 | + "text": [ |
| 80 | + "xyz\n" |
| 81 | + ] |
| 82 | + } |
| 83 | + ], |
| 84 | + "source": [ |
| 85 | + "## No you dont require an object to do this..you can specify in the class and do print\n", |
| 86 | + "class Thing3:\n", |
| 87 | + " pass\n", |
| 88 | + "val = Thing3()\n", |
| 89 | + "val.letters = 'xyz'\n", |
| 90 | + "print(val.letters)" |
| 91 | + ] |
| 92 | + }, |
| 93 | + { |
| 94 | + "cell_type": "code", |
| 95 | + "execution_count": 47, |
| 96 | + "metadata": {}, |
| 97 | + "outputs": [], |
| 98 | + "source": [ |
| 99 | + "class Element:\n", |
| 100 | + " def __init__(self, name, symbol, number):\n", |
| 101 | + " self.name = name\n", |
| 102 | + " self.symbol = symbol\n", |
| 103 | + " self.number = number\n", |
| 104 | + " ## def dump(self):\n", |
| 105 | + " ## print(self.name, self.symbol, self.number)\n", |
| 106 | + " def __str__(self):\n", |
| 107 | + " return \"name={}, symbol={}, number={}\".format(self.name, self.symbol, self.number)\n", |
| 108 | + "## creating object of the class\n", |
| 109 | + "Element1 = Element(\"Hydrogen\",\"H\",1)" |
| 110 | + ] |
| 111 | + }, |
| 112 | + { |
| 113 | + "cell_type": "code", |
| 114 | + "execution_count": 42, |
| 115 | + "metadata": {}, |
| 116 | + "outputs": [ |
| 117 | + { |
| 118 | + "name": "stdout", |
| 119 | + "output_type": "stream", |
| 120 | + "text": [ |
| 121 | + "The element name is Hydrogen\n", |
| 122 | + "The element symbol is H\n", |
| 123 | + "The element number is 1\n" |
| 124 | + ] |
| 125 | + } |
| 126 | + ], |
| 127 | + "source": [ |
| 128 | + "print(\"The element name is \",Element1.name)\n", |
| 129 | + "print(\"The element symbol is \",Element1.symbol)\n", |
| 130 | + "print(\"The element number is \",Element1.number)" |
| 131 | + ] |
| 132 | + }, |
| 133 | + { |
| 134 | + "cell_type": "code", |
| 135 | + "execution_count": 43, |
| 136 | + "metadata": {}, |
| 137 | + "outputs": [], |
| 138 | + "source": [ |
| 139 | + "element = {'name':'Hydrogen','symbol':'H','number':1}" |
| 140 | + ] |
| 141 | + }, |
| 142 | + { |
| 143 | + "cell_type": "code", |
| 144 | + "execution_count": 31, |
| 145 | + "metadata": {}, |
| 146 | + "outputs": [], |
| 147 | + "source": [ |
| 148 | + "## passed as kwargs as kwargs should be given in key value pair\n", |
| 149 | + "Hydrogen = Element(**element)" |
| 150 | + ] |
| 151 | + }, |
| 152 | + { |
| 153 | + "cell_type": "code", |
| 154 | + "execution_count": 32, |
| 155 | + "metadata": {}, |
| 156 | + "outputs": [ |
| 157 | + { |
| 158 | + "name": "stdout", |
| 159 | + "output_type": "stream", |
| 160 | + "text": [ |
| 161 | + "Hydrogen\n", |
| 162 | + "1\n", |
| 163 | + "H\n" |
| 164 | + ] |
| 165 | + } |
| 166 | + ], |
| 167 | + "source": [ |
| 168 | + "print(Hydrogen.name)\n", |
| 169 | + "print(Hydrogen.number)\n", |
| 170 | + "print(Hydrogen.symbol)" |
| 171 | + ] |
| 172 | + }, |
| 173 | + { |
| 174 | + "cell_type": "code", |
| 175 | + "execution_count": 45, |
| 176 | + "metadata": {}, |
| 177 | + "outputs": [], |
| 178 | + "source": [ |
| 179 | + "## new object created to call dump function\n", |
| 180 | + "hydrogen= Element(\"Hydrogen\",\"H\",1)" |
| 181 | + ] |
| 182 | + }, |
| 183 | + { |
| 184 | + "cell_type": "code", |
| 185 | + "execution_count": 46, |
| 186 | + "metadata": {}, |
| 187 | + "outputs": [ |
| 188 | + { |
| 189 | + "name": "stdout", |
| 190 | + "output_type": "stream", |
| 191 | + "text": [ |
| 192 | + "Hydrogen H 1\n" |
| 193 | + ] |
| 194 | + } |
| 195 | + ], |
| 196 | + "source": [ |
| 197 | + "hydrogen.dump()" |
| 198 | + ] |
| 199 | + }, |
| 200 | + { |
| 201 | + "cell_type": "code", |
| 202 | + "execution_count": 48, |
| 203 | + "metadata": {}, |
| 204 | + "outputs": [ |
| 205 | + { |
| 206 | + "name": "stdout", |
| 207 | + "output_type": "stream", |
| 208 | + "text": [ |
| 209 | + "name=Hydrogen, symbol=H, number=1\n" |
| 210 | + ] |
| 211 | + } |
| 212 | + ], |
| 213 | + "source": [ |
| 214 | + "## new object created to call _str_ function\n", |
| 215 | + "hydrogenstr= Element(\"Hydrogen\",\"H\",1)\n", |
| 216 | + "print(hydrogenstr)" |
| 217 | + ] |
| 218 | + }, |
| 219 | + { |
| 220 | + "cell_type": "code", |
| 221 | + "execution_count": 49, |
| 222 | + "metadata": {}, |
| 223 | + "outputs": [ |
| 224 | + { |
| 225 | + "name": "stdout", |
| 226 | + "output_type": "stream", |
| 227 | + "text": [ |
| 228 | + "AkashBorgalli AB 6\n" |
| 229 | + ] |
| 230 | + } |
| 231 | + ], |
| 232 | + "source": [ |
| 233 | + "class Element():\n", |
| 234 | + " def __init__(self, name, symbol, number):\n", |
| 235 | + " self.__name = name\n", |
| 236 | + " self.__symbol = symbol\n", |
| 237 | + " self.__number = number\n", |
| 238 | + " \n", |
| 239 | + " @property\n", |
| 240 | + " def name(self):\n", |
| 241 | + " return self.__name\n", |
| 242 | + "\n", |
| 243 | + " @property\n", |
| 244 | + " def symbol(self):\n", |
| 245 | + " return self.__symbol\n", |
| 246 | + " \n", |
| 247 | + " @property\n", |
| 248 | + " def number(self):\n", |
| 249 | + " return self.__number\n", |
| 250 | + " \n", |
| 251 | + "element = Element('AkashBorgalli', 'AB', 6)\n", |
| 252 | + "print(element.name, element.symbol, element.number)" |
| 253 | + ] |
| 254 | + }, |
| 255 | + { |
| 256 | + "cell_type": "code", |
| 257 | + "execution_count": 50, |
| 258 | + "metadata": {}, |
| 259 | + "outputs": [ |
| 260 | + { |
| 261 | + "name": "stdout", |
| 262 | + "output_type": "stream", |
| 263 | + "text": [ |
| 264 | + "Bear eats berries\n", |
| 265 | + "Rabbit eats clover\n", |
| 266 | + "Octothorpe eats campers\n" |
| 267 | + ] |
| 268 | + } |
| 269 | + ], |
| 270 | + "source": [ |
| 271 | + "class Bear():\n", |
| 272 | + " def eats(self):\n", |
| 273 | + " return 'berries'\n", |
| 274 | + "\n", |
| 275 | + "class Rabbit():\n", |
| 276 | + " def eats(self):\n", |
| 277 | + " return 'clover'\n", |
| 278 | + " \n", |
| 279 | + "class Octothorpe():\n", |
| 280 | + " def eats(self):\n", |
| 281 | + " return 'campers'\n", |
| 282 | + "\n", |
| 283 | + "HerbBear = Bear()\n", |
| 284 | + "print(\"Bear eats \",HerbBear.eats())\n", |
| 285 | + "cunningRabbit = Rabbit()\n", |
| 286 | + "print(\"Rabbit eats \",cunningRabbit.eats())\n", |
| 287 | + "ancientOctothorpe = Octothorpe()\n", |
| 288 | + "print(\"Octothorpe eats \",ancientOctothorpe.eats())" |
| 289 | + ] |
| 290 | + }, |
| 291 | + { |
| 292 | + "cell_type": "code", |
| 293 | + "execution_count": 51, |
| 294 | + "metadata": {}, |
| 295 | + "outputs": [ |
| 296 | + { |
| 297 | + "name": "stdout", |
| 298 | + "output_type": "stream", |
| 299 | + "text": [ |
| 300 | + "disintegrate crush ring\n" |
| 301 | + ] |
| 302 | + } |
| 303 | + ], |
| 304 | + "source": [ |
| 305 | + "class Laser():\n", |
| 306 | + " def does(self):\n", |
| 307 | + " return 'disintegrate'\n", |
| 308 | + " \n", |
| 309 | + "class Claw():\n", |
| 310 | + " def does(self):\n", |
| 311 | + " return 'crush'\n", |
| 312 | + " \n", |
| 313 | + "class SmartPhone():\n", |
| 314 | + " def does(self):\n", |
| 315 | + " return 'ring'\n", |
| 316 | + " \n", |
| 317 | + "class Robot():\n", |
| 318 | + " def __init__(self, laser, claw, smartphone):\n", |
| 319 | + " self.laser = laser\n", |
| 320 | + " self.claw = claw\n", |
| 321 | + " self.smartphone = smartphone\n", |
| 322 | + " \n", |
| 323 | + " def does(self):\n", |
| 324 | + " print(self.laser.does(), self.claw.does(), self.smartphone.does())\n", |
| 325 | + " \n", |
| 326 | + "laser = Laser()\n", |
| 327 | + "claw = Claw()\n", |
| 328 | + "smartphone = SmartPhone()\n", |
| 329 | + "robot = Robot(laser, claw, smartphone)\n", |
| 330 | + "robot.does()" |
| 331 | + ] |
| 332 | + } |
| 333 | + ], |
| 334 | + "metadata": { |
| 335 | + "kernelspec": { |
| 336 | + "display_name": "Python 3", |
| 337 | + "language": "python", |
| 338 | + "name": "python3" |
| 339 | + }, |
| 340 | + "language_info": { |
| 341 | + "codemirror_mode": { |
| 342 | + "name": "ipython", |
| 343 | + "version": 3 |
| 344 | + }, |
| 345 | + "file_extension": ".py", |
| 346 | + "mimetype": "text/x-python", |
| 347 | + "name": "python", |
| 348 | + "nbconvert_exporter": "python", |
| 349 | + "pygments_lexer": "ipython3", |
| 350 | + "version": "3.8.5" |
| 351 | + } |
| 352 | + }, |
| 353 | + "nbformat": 4, |
| 354 | + "nbformat_minor": 4 |
| 355 | +} |
0 commit comments