1 09-ModulesPackages


Previous: 08-Functions.html

09-ModulesPackages/import_soul.png
https://xkcd.com/413/

1.1 Screencasts

1.2 Extra reading

1.3 Modules, modularity, and code re-use

1.4 Import

1.5 Main

https://realpython.com/python-main-function/

#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
Details on _main_ and _name_

Sometimes we want code to be executed when a module is run directly,
but not when it is imported by another module.
if _name_ == '_main_' allows us to check whether the module is being run directly.
"""

def print_b():
    "Prints b."
    print('b')

def print_a():
    "Prints a."
    print('a')

# print_b() runs on import
print_b()

if _name_ == '_main_':
    # print_a() is only executed when the module is run directly.
    print_a()

1.6 Docstrings

"Code is more often read than written."
    - Guido van Rossum

https://www.python.org/dev/peps/pep-0257/
https://realpython.com/documenting-python-code/
Can document both functions and modules!
Put a nice string at the top of either your file, or your function.

#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
This is a summary line

There is a blank line above this one.
This module contains my_function, which does nothing!
If you import this file, then this text will print for the module's help.
There is a blank line below this, before code starts.
"""

def my_function():
    """
    Do nothing, but document it.
    No, really, it doesn't do anything.
    This prints for the function's help
    """
    pass

help(myfunction)
print(my_function._doc_)

1.7 Code

Code (to be stepped through in spyder3 and/or pudb3):
* 09-ModulesPackages/imports.tar.gz (unpack this archive to see files)

1.8 What to import, what to write?

Sometimes, it’s good to import what does not need to be written:
https://en.wikipedia.org/wiki/Standing_on_the_shoulders_of_giants

However, you should keep your dependencies minimal, even doing a little work and thinking for yourself if you must (gasp, really??).
09-ModulesPackages/modern-software-development-animation.gif

Next: 10-DebuggingTesting.html