Skip to content

Commit 245e233

Browse files
committed
first class functions and closures are added
1 parent 775200e commit 245e233

File tree

3 files changed

+384
-21
lines changed

3 files changed

+384
-21
lines changed

.ipynb_checkpoints/13. Generators and Decorators-checkpoint.ipynb

Lines changed: 230 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -173,25 +173,23 @@
173173
]
174174
},
175175
{
176-
"cell_type": "code",
177-
"execution_count": null,
178-
"metadata": {
179-
"collapsed": true
180-
},
181-
"outputs": [],
176+
"cell_type": "markdown",
177+
"metadata": {},
182178
"source": [
183-
"def read_big_file_in_chunks(file_object, chunk_size=1024):\n",
184-
" \"\"\"Reading whole big file in chunks.\"\"\"\n",
185-
" while True:\n",
186-
" data = file_object.read(chunk_size)\n",
187-
" if not data:\n",
188-
" break\n",
189-
" yield data\n",
190-
"\n",
191-
"\n",
192-
"f = open('very_very_big_file.log')\n",
193-
"for chunk in read_in_chunks(f):\n",
194-
" process_data(chunck)\n"
179+
"```\n",
180+
" def read_big_file_in_chunks(file_object, chunk_size=1024):\n",
181+
" \"\"\"Reading whole big file in chunks.\"\"\"\n",
182+
" while True:\n",
183+
" data = file_object.read(chunk_size)\n",
184+
" if not data:\n",
185+
" break\n",
186+
" yield data\n",
187+
"\n",
188+
"\n",
189+
" f = open('very_very_big_file.log')\n",
190+
" for chunk in read_big_file_in_chunks(f):\n",
191+
" process_data(chunck)\n",
192+
"```"
195193
]
196194
},
197195
{
@@ -329,6 +327,220 @@
329327
"\n"
330328
]
331329
},
330+
{
331+
"cell_type": "code",
332+
"execution_count": 6,
333+
"metadata": {
334+
"collapsed": true
335+
},
336+
"outputs": [],
337+
"source": [
338+
"def smart_divide(func):\n",
339+
" def inner(a,b):\n",
340+
" print(\"I am going to divide\",a,\"and\",b)\n",
341+
" if b == 0:\n",
342+
" print(\"Whoops! cannot divide\")\n",
343+
" return\n",
344+
"\n",
345+
" return func(a,b)\n",
346+
" return inner\n",
347+
"\n",
348+
"@smart_divide\n",
349+
"def divide(a,b):\n",
350+
" return a/b"
351+
]
352+
},
353+
{
354+
"cell_type": "code",
355+
"execution_count": 7,
356+
"metadata": {},
357+
"outputs": [
358+
{
359+
"name": "stdout",
360+
"output_type": "stream",
361+
"text": [
362+
"I am going to divide 9 and 0\n",
363+
"Whoops! cannot divide\n"
364+
]
365+
}
366+
],
367+
"source": [
368+
"divide(9,0)"
369+
]
370+
},
371+
{
372+
"cell_type": "markdown",
373+
"metadata": {
374+
"collapsed": true
375+
},
376+
"source": [
377+
"### EXAMPLE - 1"
378+
]
379+
},
380+
{
381+
"cell_type": "code",
382+
"execution_count": 3,
383+
"metadata": {},
384+
"outputs": [
385+
{
386+
"name": "stdout",
387+
"output_type": "stream",
388+
"text": [
389+
"Entering call_func1\n",
390+
"inside func1()\n",
391+
"Exited call_func1\n",
392+
"Entering call_func2\n",
393+
"inside func2()\n",
394+
"Exited call_func2\n",
395+
"new_fun\n"
396+
]
397+
}
398+
],
399+
"source": [
400+
"# PythonDecorators/entry_exit_function.py\n",
401+
"def my_decorator(f):\n",
402+
" def new_fun():\n",
403+
" print(\"Entering\", f.__name__)\n",
404+
" f()\n",
405+
" print(\"Exited\", f.__name__)\n",
406+
" return new_fun\n",
407+
"\n",
408+
"@my_decorator\n",
409+
"def call_func1():\n",
410+
" print(\"inside func1()\")\n",
411+
"\n",
412+
"@my_decorator\n",
413+
"def call_func2():\n",
414+
" print(\"inside func2()\")\n",
415+
"\n",
416+
"call_func1()\n",
417+
"call_func2()\n",
418+
"print(call_func1.__name__)"
419+
]
420+
},
421+
{
422+
"cell_type": "markdown",
423+
"metadata": {},
424+
"source": [
425+
"### NOTE\n",
426+
"\n",
427+
" - new_fun() is defined within the body of my_decorator(), so it is created and returned when my_decorator() is called.\n",
428+
" - Note that new_fun() is a closure, because it captures the actual value of f.\n",
429+
" - print(call_func1.__name__) prints 'new_fun' because new_fun function has been substituted for the original function during decoration"
430+
]
431+
},
432+
{
433+
"cell_type": "markdown",
434+
"metadata": {},
435+
"source": [
436+
"### EXAMPLE - 2"
437+
]
438+
},
439+
{
440+
"cell_type": "code",
441+
"execution_count": 4,
442+
"metadata": {},
443+
"outputs": [
444+
{
445+
"name": "stdout",
446+
"output_type": "stream",
447+
"text": [
448+
"<i><b>Hello World</b></i>\n"
449+
]
450+
}
451+
],
452+
"source": [
453+
"def makebold(fn):\n",
454+
" def wrapped():\n",
455+
" return \"<b>%s</b>\" % fn()\n",
456+
" return wrapped\n",
457+
"\n",
458+
"def makeitalic(fn):\n",
459+
" def cover():\n",
460+
" return \"<i>%s</i>\" %fn()\n",
461+
" return cover\n",
462+
"\n",
463+
"@makeitalic\n",
464+
"@makebold\n",
465+
"def hello():\n",
466+
" return \"Hello World\"\n",
467+
"\n",
468+
"print (hello())"
469+
]
470+
},
471+
{
472+
"cell_type": "markdown",
473+
"metadata": {
474+
"collapsed": true
475+
},
476+
"source": [
477+
"### EXAMPLE - 3"
478+
]
479+
},
480+
{
481+
"cell_type": "markdown",
482+
"metadata": {},
483+
"source": [
484+
"```\n",
485+
"@testtime\n",
486+
"@testlog\n",
487+
"def call_me():\n",
488+
" print (\"Hello, world!\")```\n",
489+
" \n",
490+
" \n",
491+
" \n",
492+
" - create function object (say - helloObject ) and binds it to name 'call_me'\n",
493+
" - passes helloObject to 'testlog' which will return new function object (say - helloObject2)\n",
494+
" - interpreter binds the real name 'call_me' to this new hellObject2.\n",
495+
" - helloObject2 is passed to 'testtime' and it will return new function object (say - hellObject3)\n",
496+
" - again, interpreter binds orignal name to this new object\n",
497+
" - IMPORTANT : we wrapped helloObject from call_me to helloObject2 from testlog and finally wrap this to helloObject3, and bound the original name 'call_me' to helloObject3. So when we call 'call_me', we are actually calling helloObject3."
498+
]
499+
},
500+
{
501+
"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+
],
518+
"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()"
542+
]
543+
},
332544
{
333545
"cell_type": "code",
334546
"execution_count": null,

0 commit comments

Comments
 (0)