Previous: 08-Functions.html
document.querySelector('video').playbackRate = 1.2
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()
"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_)
Code (to be stepped through in spyder3 and/or pudb3):
* 09-ModulesPackages/imports.tar.gz
(unpack this archive to see files)
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??).
Next: 10-DebuggingTesting.html