|
192 | 192 | "```"
|
193 | 193 | ]
|
194 | 194 | },
|
| 195 | + { |
| 196 | + "cell_type": "markdown", |
| 197 | + "metadata": {}, |
| 198 | + "source": [ |
| 199 | + "### 2. First Class Functions\n", |
| 200 | + "\n", |
| 201 | + " - treating functions like other normal entities\n", |
| 202 | + " - passing function as an argument, assigning to a variable, returning from a function" |
| 203 | + ] |
| 204 | + }, |
| 205 | + { |
| 206 | + "cell_type": "code", |
| 207 | + "execution_count": 22, |
| 208 | + "metadata": {}, |
| 209 | + "outputs": [ |
| 210 | + { |
| 211 | + "name": "stdout", |
| 212 | + "output_type": "stream", |
| 213 | + "text": [ |
| 214 | + "['Mr Kumar', 'Mrs Sabi']\n" |
| 215 | + ] |
| 216 | + } |
| 217 | + ], |
| 218 | + "source": [ |
| 219 | + "# EXAMPLE\n", |
| 220 | + "\n", |
| 221 | + "def append_prefix(data):\n", |
| 222 | + " if data[0] == 'male':\n", |
| 223 | + " return 'Mr ' + data[1]\n", |
| 224 | + " if data[0] == 'female':\n", |
| 225 | + " return 'Mrs ' + data[1]\n", |
| 226 | + "\n", |
| 227 | + " \n", |
| 228 | + "def map_function(func, my_list):\n", |
| 229 | + " final_output = []\n", |
| 230 | + " for each in my_list:\n", |
| 231 | + " final_output.append(func(each))\n", |
| 232 | + " return final_output\n", |
| 233 | + "\n", |
| 234 | + "output = map_function(append_prefix, [('male', 'Kumar'), ('female', 'Sabi')])\n", |
| 235 | + "print(output)\n", |
| 236 | + " " |
| 237 | + ] |
| 238 | + }, |
| 239 | + { |
| 240 | + "cell_type": "markdown", |
| 241 | + "metadata": {}, |
| 242 | + "source": [ |
| 243 | + "### 3. Closures\n" |
| 244 | + ] |
| 245 | + }, |
| 246 | + { |
| 247 | + "cell_type": "code", |
| 248 | + "execution_count": 32, |
| 249 | + "metadata": { |
| 250 | + "collapsed": true |
| 251 | + }, |
| 252 | + "outputs": [], |
| 253 | + "source": [ |
| 254 | + "def greeting(prefix):\n", |
| 255 | + " \n", |
| 256 | + " def append_msg(msg):\n", |
| 257 | + " print ('{0}{1}{2}'.format(prefix,' ', msg))\n", |
| 258 | + " return append_msg ## returning without execution" |
| 259 | + ] |
| 260 | + }, |
| 261 | + { |
| 262 | + "cell_type": "code", |
| 263 | + "execution_count": 35, |
| 264 | + "metadata": {}, |
| 265 | + "outputs": [ |
| 266 | + { |
| 267 | + "name": "stdout", |
| 268 | + "output_type": "stream", |
| 269 | + "text": [ |
| 270 | + "<function greeting.<locals>.append_msg at 0x7f0c003cd488>\n", |
| 271 | + "append_msg\n", |
| 272 | + "Mr. Kumar\n", |
| 273 | + "Mr. Kabi\n" |
| 274 | + ] |
| 275 | + } |
| 276 | + ], |
| 277 | + "source": [ |
| 278 | + "first = greeting('Mr.')\n", |
| 279 | + "\n", |
| 280 | + "print(first)\n", |
| 281 | + "print(first.__name__)\n", |
| 282 | + "\n", |
| 283 | + "# REMEMBER THE PARAMETER 'Mr.' even after outer function is executed already\n", |
| 284 | + "first('Kumar')\n", |
| 285 | + "first('Kabi')" |
| 286 | + ] |
| 287 | + }, |
| 288 | + { |
| 289 | + "cell_type": "code", |
| 290 | + "execution_count": 30, |
| 291 | + "metadata": {}, |
| 292 | + "outputs": [ |
| 293 | + { |
| 294 | + "name": "stdout", |
| 295 | + "output_type": "stream", |
| 296 | + "text": [ |
| 297 | + "Mrs. Kumari\n", |
| 298 | + "Mrs. Kabita\n" |
| 299 | + ] |
| 300 | + } |
| 301 | + ], |
| 302 | + "source": [ |
| 303 | + "second = greeting('Mrs.')\n", |
| 304 | + "second('Kumari')\n", |
| 305 | + "second('Kabita')" |
| 306 | + ] |
| 307 | + }, |
195 | 308 | {
|
196 | 309 | "cell_type": "markdown",
|
197 | 310 | "metadata": {
|
198 | 311 | "collapsed": true
|
199 | 312 | },
|
200 | 313 | "source": [
|
201 |
| - "### 2. DECORATORS\n", |
| 314 | + "### 4. DECORATORS\n", |
202 | 315 | "\n",
|
203 | 316 | "There may be a scenario to perform any tasks before / after executing a function. This can be achieved through Decorators.\n",
|
204 | 317 | "\n",
|
|
213 | 326 | "cell_type": "markdown",
|
214 | 327 | "metadata": {},
|
215 | 328 | "source": [
|
216 |
| - "#### 2.1 EXAMPLE - 1" |
| 329 | + "### Illustration" |
| 330 | + ] |
| 331 | + }, |
| 332 | + { |
| 333 | + "cell_type": "code", |
| 334 | + "execution_count": 12, |
| 335 | + "metadata": {}, |
| 336 | + "outputs": [ |
| 337 | + { |
| 338 | + "name": "stdout", |
| 339 | + "output_type": "stream", |
| 340 | + "text": [ |
| 341 | + "I am executed before original function\n", |
| 342 | + "hey I am wrapped by decorator\n" |
| 343 | + ] |
| 344 | + } |
| 345 | + ], |
| 346 | + "source": [ |
| 347 | + "# define a decorator \n", |
| 348 | + "def decorator_func(original_func):\n", |
| 349 | + " def wrapper_func():\n", |
| 350 | + " # without modifying original func, I can add more functionality\n", |
| 351 | + " print (\"I am executed before original function\")\n", |
| 352 | + " return original_func()\n", |
| 353 | + " return wrapper_func\n", |
| 354 | + "\n", |
| 355 | + "# define your working function\n", |
| 356 | + "\n", |
| 357 | + "@decorator_func\n", |
| 358 | + "def my_func():\n", |
| 359 | + " print('hey I am wrapped by decorator')\n", |
| 360 | + "\n", |
| 361 | + " \n", |
| 362 | + "my_func()" |
| 363 | + ] |
| 364 | + }, |
| 365 | + { |
| 366 | + "cell_type": "code", |
| 367 | + "execution_count": 17, |
| 368 | + "metadata": {}, |
| 369 | + "outputs": [ |
| 370 | + { |
| 371 | + "name": "stdout", |
| 372 | + "output_type": "stream", |
| 373 | + "text": [ |
| 374 | + "I am executed before original function\n", |
| 375 | + "hey I am wrapped by decorator\n" |
| 376 | + ] |
| 377 | + } |
| 378 | + ], |
| 379 | + "source": [ |
| 380 | + "# define a decorator \n", |
| 381 | + "def decorator_func(original_func):\n", |
| 382 | + " def wrapper_func(*args, **kwargs):\n", |
| 383 | + " # without modifying original func, I can add more functionality\n", |
| 384 | + " print (\"I am executed before original function\")\n", |
| 385 | + " return original_func(*args, **kwargs)\n", |
| 386 | + " return wrapper_func\n", |
| 387 | + "\n", |
| 388 | + "# define your working function\n", |
| 389 | + "\n", |
| 390 | + "@decorator_func\n", |
| 391 | + "def my_func_para(param1, param2):\n", |
| 392 | + " print('hey I am wrapped by decorator')\n", |
| 393 | + "\n", |
| 394 | + " \n", |
| 395 | + "my_func_para('hey', 'buddy')" |
| 396 | + ] |
| 397 | + }, |
| 398 | + { |
| 399 | + "cell_type": "markdown", |
| 400 | + "metadata": {}, |
| 401 | + "source": [ |
| 402 | + "#### EXAMPLE - 1" |
217 | 403 | ]
|
218 | 404 | },
|
219 | 405 | {
|
|
374 | 560 | "collapsed": true
|
375 | 561 | },
|
376 | 562 | "source": [
|
377 |
| - "### EXAMPLE - 1" |
| 563 | + "### EXAMPLE - 2" |
378 | 564 | ]
|
379 | 565 | },
|
380 | 566 | {
|
|
438 | 624 | },
|
439 | 625 | {
|
440 | 626 | "cell_type": "code",
|
441 |
| - "execution_count": 4, |
| 627 | + "execution_count": 18, |
442 | 628 | "metadata": {},
|
443 | 629 | "outputs": [
|
444 | 630 | {
|
445 | 631 | "name": "stdout",
|
446 | 632 | "output_type": "stream",
|
447 | 633 | "text": [
|
| 634 | + "inside italic wrapped\n", |
| 635 | + "inside bold hello\n", |
448 | 636 | "<i><b>Hello World</b></i>\n"
|
449 | 637 | ]
|
450 | 638 | }
|
451 | 639 | ],
|
452 | 640 | "source": [
|
453 | 641 | "def makebold(fn):\n",
|
| 642 | + " print('makebold called')\n", |
454 | 643 | " def wrapped():\n",
|
| 644 | + " print('inside bold', fn.__name__)\n", |
| 645 | + " \n", |
455 | 646 | " return \"<b>%s</b>\" % fn()\n",
|
456 | 647 | " return wrapped\n",
|
457 | 648 | "\n",
|
458 | 649 | "def makeitalic(fn):\n",
|
459 | 650 | " def cover():\n",
|
| 651 | + " print('inside italic', fn.__name__)\n", |
| 652 | + " \n", |
460 | 653 | " return \"<i>%s</i>\" %fn()\n",
|
461 | 654 | " return cover\n",
|
462 | 655 | "\n",
|
|
499 | 692 | },
|
500 | 693 | {
|
501 | 694 | "cell_type": "code",
|
502 |
| - "execution_count": 4, |
503 |
| - "metadata": {}, |
504 |
| - "outputs": [ |
505 |
| - { |
506 |
| - "name": "stdout", |
507 |
| - "output_type": "stream", |
508 |
| - "text": [ |
509 |
| - "third one\n", |
510 |
| - "second one\n", |
511 |
| - "first one\n", |
512 |
| - "Hello, world!\n", |
513 |
| - "Hello, solar system!\n", |
514 |
| - "Hello, galaxy!\n" |
515 |
| - ] |
516 |
| - } |
517 |
| - ], |
| 695 | + "execution_count": null, |
| 696 | + "metadata": { |
| 697 | + "collapsed": true |
| 698 | + }, |
| 699 | + "outputs": [], |
518 | 700 | "source": [
|
519 |
| - "def testtime(original_function):\n", |
520 |
| - " def new_function():\n", |
521 |
| - " print('second one')\n", |
522 |
| - " #original_function() # the () after \"original_function\" causes original_function to be called\n", |
523 |
| - " print(\"Hello, solar system!\")\n", |
524 |
| - " return new_function\n", |
525 |
| - " \n", |
526 |
| - "def testlog(original_function):\n", |
527 |
| - " def new_function():\n", |
528 |
| - " print('third one')\n", |
529 |
| - " #original_function() # the parentheses after \"original_function\" cause original_function to be called\n", |
530 |
| - " print(\"Hello, galaxy!\")\n", |
531 |
| - " return new_function\n", |
532 |
| - " \n", |
533 |
| - "\n", |
534 |
| - "@testlog\n", |
535 |
| - "@testtime\n", |
536 |
| - "def call_me():\n", |
537 |
| - " print('first one')\n", |
538 |
| - " print (\"Hello, world!\")\n", |
539 |
| - " \n", |
540 |
| - "# Here is where we actually *do* something!\n", |
541 |
| - "call_me()" |
| 701 | + "def counter()" |
542 | 702 | ]
|
543 | 703 | },
|
544 | 704 | {
|
|
0 commit comments