|
62 | 62 | "- [1. LG - Local vs. Global](#section_1) \n", |
63 | 63 | "- [2. LEG - Local, Enclosed, and Global scope](#section_2) \n", |
64 | 64 | "- [3. LEGB - Local, Enclosed, Global, Built-in](#section_3) \n", |
| 65 | + "- [Self-assessment exercise](#assessment)\n", |
65 | 66 | "- [Conclusion](#conclusion) \n", |
66 | 67 | "- [Solutions](#solutions)" |
67 | 68 | ] |
|
498 | 499 | "input": [ |
499 | 500 | "a_var = 'global variable'\n", |
500 | 501 | "\n", |
501 | | - "\n", |
502 | | - "\n", |
503 | 502 | "def len(in_var):\n", |
504 | 503 | " print('called my len() function')\n", |
505 | 504 | " l = 0\n", |
|
511 | 510 | " len_in_var = len(in_var)\n", |
512 | 511 | " print('Input variable is of length', len_in_var)\n", |
513 | 512 | "\n", |
514 | | - "#a_func('Hello, World!')\n", |
515 | | - " " |
| 513 | + "#a_func('Hello, World!')" |
516 | 514 | ], |
517 | 515 | "language": "python", |
518 | 516 | "metadata": {}, |
|
550 | 548 | "Since the exact same names can be used to map names to different objects - as long as the names are in different name spaces - there is no problem of reusing the name `len` to define our own length function (this is just for demonstration pruposes, it is NOT recommended). As we go up in Python's L -> E -> G -> B hierarchy, the function `a_func()` finds `len()` already in the global scope first before it attempts" |
551 | 549 | ] |
552 | 550 | }, |
| 551 | + { |
| 552 | + "cell_type": "markdown", |
| 553 | + "metadata": {}, |
| 554 | + "source": [ |
| 555 | + "<a name =\"assessment\"></a>\n", |
| 556 | + "<br>\n", |
| 557 | + "<br>" |
| 558 | + ] |
| 559 | + }, |
| 560 | + { |
| 561 | + "cell_type": "markdown", |
| 562 | + "metadata": {}, |
| 563 | + "source": [ |
| 564 | + "# Self-assessment exercise" |
| 565 | + ] |
| 566 | + }, |
| 567 | + { |
| 568 | + "cell_type": "markdown", |
| 569 | + "metadata": {}, |
| 570 | + "source": [ |
| 571 | + "Now, after we went through a couple of exercises, let us quickly check where we are. So, one more time: What would the following code print out?" |
| 572 | + ] |
| 573 | + }, |
| 574 | + { |
| 575 | + "cell_type": "code", |
| 576 | + "collapsed": false, |
| 577 | + "input": [ |
| 578 | + "a = 'global'\n", |
| 579 | + "\n", |
| 580 | + "def outer():\n", |
| 581 | + " \n", |
| 582 | + " def len(in_var):\n", |
| 583 | + " print('called my len() function: ', end=\"\")\n", |
| 584 | + " l = 0\n", |
| 585 | + " for i in in_var:\n", |
| 586 | + " l += 1\n", |
| 587 | + " return l\n", |
| 588 | + " \n", |
| 589 | + " a = 'local'\n", |
| 590 | + " \n", |
| 591 | + " def inner():\n", |
| 592 | + " global len\n", |
| 593 | + " nonlocal a\n", |
| 594 | + " a += ' variable'\n", |
| 595 | + " inner()\n", |
| 596 | + " print('a is', a)\n", |
| 597 | + " print(len(a))\n", |
| 598 | + "\n", |
| 599 | + "\n", |
| 600 | + "outer()\n", |
| 601 | + "\n", |
| 602 | + "print(len(a))\n", |
| 603 | + "print('a is', a)" |
| 604 | + ], |
| 605 | + "language": "python", |
| 606 | + "metadata": {}, |
| 607 | + "outputs": [], |
| 608 | + "prompt_number": 59 |
| 609 | + }, |
553 | 610 | { |
554 | 611 | "cell_type": "markdown", |
555 | 612 | "metadata": {}, |
|
559 | 616 | "<br>" |
560 | 617 | ] |
561 | 618 | }, |
| 619 | + { |
| 620 | + "cell_type": "markdown", |
| 621 | + "metadata": {}, |
| 622 | + "source": [ |
| 623 | + "[[go to solution](#solutions)]" |
| 624 | + ] |
| 625 | + }, |
562 | 626 | { |
563 | 627 | "cell_type": "markdown", |
564 | 628 | "metadata": {}, |
|
675 | 739 | "metadata": {}, |
676 | 740 | "outputs": [], |
677 | 741 | "prompt_number": 9 |
| 742 | + }, |
| 743 | + { |
| 744 | + "cell_type": "code", |
| 745 | + "collapsed": false, |
| 746 | + "input": [ |
| 747 | + "# Execute to run the self-assessment solution\n", |
| 748 | + "\n", |
| 749 | + "sol = \"000010100110111101110101011101000110010101110010001010\"\\\n", |
| 750 | + "\"0000101001001110100000101000001010011000010010000001101001011100110\"\\\n", |
| 751 | + "\"0100000011011000110111101100011011000010110110000100000011101100110\"\\\n", |
| 752 | + "\"0001011100100110100101100001011000100110110001100101000010100110001\"\\\n", |
| 753 | + "\"1011000010110110001101100011001010110010000100000011011010111100100\"\\\n", |
| 754 | + "\"1000000110110001100101011011100010100000101001001000000110011001110\"\\\n", |
| 755 | + "\"1010110111001100011011101000110100101101111011011100011101000100000\"\\\n", |
| 756 | + "\"0011000100110100000010100000101001100111011011000110111101100010011\"\\\n", |
| 757 | + "\"0000101101100001110100000101000001010001101100000101001100001001000\"\\\n", |
| 758 | + "\"0001101001011100110010000001100111011011000110111101100010011000010\"\\\n", |
| 759 | + "\"1101100\"\n", |
| 760 | + "\n", |
| 761 | + "sol_str =''.join(chr(int(sol[i:i+8], 2)) for i in range(0, len(sol), 8))\n", |
| 762 | + "for line in sol_str.split('\\n'):\n", |
| 763 | + " print(line)" |
| 764 | + ], |
| 765 | + "language": "python", |
| 766 | + "metadata": {}, |
| 767 | + "outputs": [], |
| 768 | + "prompt_number": 58 |
678 | 769 | } |
679 | 770 | ], |
680 | 771 | "metadata": {} |
|
0 commit comments