8000 updating on modules · naiks/Complete-Python-3-Bootcamp@1cef96f · GitHub
[go: up one dir, main page]

Skip to content

Commit 1cef96f

Browse files
committed
updating on modules
1 parent 680c67a commit 1cef96f
  • 08-Milestone Project - 2
  • 32 files changed

    +148
    -24
    lines changed

    03-Methods and Functions/.ipynb_checkpoints/05-Lambda-Expressions-Map-and-Filter-checkpoint.ipynb

    Lines changed: 1 addition & 1 deletion
    Original file line numberDiff line numberDiff line change
    @@ -217,7 +217,7 @@
    217217
    "source": [
    218218
    "## lambda expression\n",
    219219
    "\n",
    220-
    "One of Pythons most useful (and for recruits, confusing) tools is the lambda expression. lambda expressions allow us to create \"anonymous\" functions. This basically means we can quickly make ad-hoc functions without needing to properly define a function using def.\n",
    220+
    "One of Pythons most useful (and for beginners, confusing) tools is the lambda expression. lambda expressions allow us to create \"anonymous\" functions. This basically means we can quickly make ad-hoc functions without needing to properly define a function using def.\n",
    221221
    "\n",
    222222
    "Function objects returned by running lambda expressions work exactly the same as those created and assigned by defs. There is key difference that makes lambda useful in specialized roles:\n",
    223223
    "\n",

    03-Methods and Functions/05-Lambda-Expressions-Map-and-Filter.ipynb

    Lines changed: 1 addition & 1 deletion
    Original file line numberDiff line numberDiff line change
    @@ -217,7 +217,7 @@
    217217
    "source": [
    218218
    "## lambda expression\n",
    219219
    "\n",
    220-
    "One of Pythons most useful (and for recruits, confusing) tools is the lambda expression. lambda expressions allow us to create \"anonymous\" functions. This basically means we can quickly make ad-hoc functions without needing to properly define a function using def.\n",
    220+
    "One of Pythons most useful (and for beginners, confusing) tools is the lambda expression. lambda expressions allow us to create \"anonymous\" functions. This basically means we can quickly make ad-hoc functions without needing to properly define a function using def.\n",
    221221
    "\n",
    222222
    "Function objects returned by running lambda expressions work exactly the same as those created and assigned by defs. There is key difference that makes lambda useful in specialized roles:\n",
    223223
    "\n",

    04-Milestone Project - 1/03-Milestone Project 1 - Complete Walkthrough Solution.ipynb

    Lines changed: 12 additions & 22 deletions
    Original file line numberDiff line numberDiff line change
    @@ -52,9 +52,7 @@
    5252
    {
    5353
    "cell_type": "code",
    5454
    "execution_count": 2,
    55-
    "metadata": {
    56-
    "collapsed": false
    57-
    },
    55+
    "metadata": {},
    5856
    "outputs": [
    5957
    {
    6058
    "name": "stdout",
    @@ -116,9 +114,7 @@
    116114
    {
    117115
    "cell_type": "code",
    118116
    "execution_count": 4,
    119-
    "metadata": {
    120-
    "collapsed": false
    121-
    },
    117+
    "metadata": {},
    122118
    "outputs": [
    123119
    {
    124120
    "name": "stdout",
    @@ -171,9 +167,7 @@
    171167
    {
    172168
    "cell_type": "code",
    173169
    "execution_count": 6,
    174-
    "metadata": {
    175-
    "collapsed": false
    176-
    },
    170+
    "metadata": {},
    177171
    "outputs": [
    178172
    {
    179173
    "name": "stdout",
    @@ -235,9 +229,7 @@
    235229
    {
    236230
    "cell_type": "code",
    237231
    "execution_count": 8,
    238-
    "metadata": {
    239-
    "collapsed": false
    240-
    },
    232+
    "metadata": {},
    241233
    "outputs": [
    242234
    {
    243235
    "data": {
    @@ -375,27 +367,25 @@
    375367
    },
    376368
    {
    377369
    "cell_type": "code",
    378-
    "execution_count": 16,
    379-
    "metadata": {
    380-
    "collapsed": false
    381-
    },
    370+
    "execution_count": 14,
    371+
    "metadata": {},
    382372
    "outputs": [
    383373
    {
    384374
    "name": "stdout",
    385375
    "output_type": "stream",
    386376
    "text": [
    387377
    " | |\n",
    388-
    " | O | \n",
    378+
    " | O | O\n",
    389379
    " | |\n",
    390380
    "-----------\n",
    391381
    " | |\n",
    392-
    " | O | X\n",
    382+
    " | | \n",
    393383
    " | |\n",
    394384
    "-----------\n",
    395385
    " | |\n",
    396-
    " X | O | X\n",
    386+
    " X | X | X\n",
    397387
    " | |\n",
    398-
    "Player 2 has won!\n",
    388+
    "Congratulations! You have won the game!\n",
    399389
    "Do you want to play again? Enter Yes or No: No\n"
    400390
    ]
    401391
    }
    @@ -473,7 +463,7 @@
    473463
    "metadata": {
    474464
    "anaconda-cloud": {},
    475465
    "kernelspec": {
    476-
    "display_name": "Python [default]",
    466+
    "display_name": "Python 3",
    477467
    "language": "python",
    478468
    "name": "python3"
    479469
    },
    @@ -487,7 +477,7 @@
    487477
    "name": "python",
    488478
    "nbconvert_exporter": "python",
    489479
    "pygments_lexer": "ipython3",
    490-
    "version": "3.5.3"
    480+
    "version": "3.6.1"
    491481
    }
    492482
    },
    493483
    "nbformat": 4,
    Lines changed: 5 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -0,0 +1,5 @@
    1+
    # This is the module we will be importing from!
    2+
    # Great resource: https://docs.python.org/3/tutorial/modules.html
    3+
    4+
    def func_in_mymodule():
    5+
    print("I am a function inside of the mymodule.py file!")
    Lines changed: 34 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -0,0 +1,34 @@
    1+
    ################
    2+
    # Example Three:
    3+
    ################
    4+
    # Uncomment this and comment everything else to run!
    5+
    6+
    # import mymodule
    7+
    # mymodule.func_in_mymodule()
    8+
    9+
    ################
    10+
    # Example Two:
    11+
    ################
    12+
    # Uncomment this and comment everything else to run!
    13+
    14+
    # import mymodule as mm
    15+
    # mm.func_in_mymodule()
    16+
    17+
    ################
    18+
    # Example Three:
    19+
    ################
    20+
    # Uncomment this and comment everything else to run!
    21+
    22+
    # from mymodule import func_in_mymodule
    23+
    # func_in_mymodule()
    24+
    25+
    ################
    26+
    # Example Four:
    27+
    ################
    28+
    # Uncomment this and comment everything else to run!
    29+
    30+
    # This is posisble but frowned upon, often causes poorly readable code because
    31+
    # you don't know what functions come from mymodule
    32+
    33+
    # from mymodule import *
    34+
    # func_in_mymodule()
    Lines changed: 76 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -0,0 +1,76 @@
    1+
    Sometimes when you are importing from a module, you would like to know whether
    2+
    a modules function is being used as an import, or if you are using the original
    3+
    .py file of that module. In this case we can use the:
    4+
    5+
    if __name__ == "__main__":
    6+
    7+
    line to determine this. For example:
    8+
    9+
    When your script is run by passing it as a command to the Python interpreter:
    10+
    11+
    python myscript.py
    12+
    13+
    all of the code that is at indentation level 0 gets executed. Functions and
    14+
    classes that are defined are, well, defined, but none of their code gets ran.
    15+
    Unlike other languages, there's no main() function that gets run automatically
    16+
    - the main() function is implicitly all the code at the top level.
    17+
    18+
    In this case, the top-level code is an if block. __name__ is a built-in variable
    19+
    which evaluate to the name of the current module. However, if a module is being
    20+
    run directly (as in myscript.py above), then __name__ instead is set to the
    21+
    string "__main__". Thus, you can test whether your script is being run directly
    22+
    or being imported by something else by testing
    23+
    24+
    if __name__ == "__main__":
    25+
    ...
    26+
    27+
    If that code is being imported into another module, the various function and
    28+
    class definitions will be imported, but the main() code won't get run. As a
    29+
    basic example, consider the following two scripts:
    30+
    31+
    # file one.py
    32+
    def func():
    33+
    print("func() in one.py")
    34+
    35+
    print("top-level in one.py")
    36+
    37+
    if __name__ == "__main__":
    38+
    print("one.py is being run directly")
    39+
    else:
    40+
    print("one.py is being imported into another module")
    41+
    42+
    and then:
    43+
    44+
    # file two.py
    45+
    import one
    46+
    47+
    print("top-level in two.py")
    48+
    one.func()
    49+
    50+
    if __name__ == "__main__":
    51+
    print("two.py is being run directly")
    52+
    else:
    53+
    print("two.py is being imported into another module")
    54+
    55+
    Now, if you invoke the interpreter as
    56+
    57+
    python one.py
    58+
    59+
    The output will be
    60+
    61+
    top-level in one.py
    62+
    63+
    one.py is being run directly
    64+
    If you run two.py instead:
    65+
    66+
    python two.py
    67+
    68+
    You get
    69+
    70+
    top-level in one.py
    71+
    one.py is being imported into another module
    72+
    top-level in two.py
    73+
    func() in one.py
    74+
    two.py is being run directly
    75+
    76+
    Thus, when module one gets loaded, its __name__ equals "one" instead of __main__.
    Lines changed: 9 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -0,0 +1,9 @@
    1+
    def func():
    2+
    print("func() ran in one.py")
    3+
    4+
    print("top-level print inside of one.py")
    5+
    6+
    if __name__ == "__main__":
    7+
    print("one.py is being run directly")
    8+
    else:
    9+
    print("one.py is being imported into another module")
    Lines changed: 10 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -0,0 +1,10 @@
    1+
    import one
    2+
    3+
    print("top-level in two.py")
    4+
    5+
    one.func()
    6+
    7+
    if __name__ == "__main__":
    8+
    print("two.py is being run directly")
    9+
    else:
    10+
    print("two.py is being imported into another module")

    0 commit comments

    Comments
     (0)
    0