View on GitHub

reading-notes

Daily Reading 2

Testing and Modules.

What are the key principles of Test-Driven Development (TDD) in Python, and how do they contribute to the overall quality of code?

Test-Driven Development is a software development style where every second step of creating your program is to make a test case that will cause it to fail. Then, from that failure condition, rework the code to make the program pass your test with that input, then invent a new test that will cause it to fail again, until every use case that the programmer can come up with for possible inputs will pass the tests.

Through this particular routine, you end up going through every input a user could make, and verify that each of them work, which results in the only bugs that can make it through your testing are ones that relate to user inputs that were never considered by the programmer as possible. You can be sure that your program behaves as expected for all of your inputs, because you’ve already verified each and every one you could come up with.

Explain the purpose of the if __name__ == '__main__': statement in Python scripts. What are some use cases for including this conditional in your code?

if __name__ == '__main__': is a conditional in python that will be true if the python program is executed by name directly, and will be false if executed as part of a different python program. This can be used to provide error messages to the user if the wrong thing is run directly, such as This program (branching-paths.py) is not intended to be run by the user, please run game.py instead! or to provide diagnostic information to the developer/differing functionality based on if the program is interacted with directly.

Issues can also arise with programs that execute other programs as modules in python where a module program executed by a main program can end up being run twice as a result of direct-run diagnostic tests, so having any diagnostics sequestered behind if __name__ == '__main__': will stop them from being run during normal use.

Describe the concept of recursion in Python.

Recursion, in the context of python is the name of the behavior exhibited when you invoke a function, that contains within itself, a call to execute itself. Infinite recursion, much in the way of infinite loops, will lead to your program getting stuck going in a circle, and eventually crash to prevent your computer from being trapped forever attempting to perform an action that will never complete. Each time a function calls itself from within, it will go one level deeper into recursion, which raises the question, what “depth” or “version” of the function will be run, in what order?

The standard answer for programming is “depth first” where whenever the recursive function is called, the new deeper version is immediately run and solved, (unless it is requested to go deeper again, in which case it will wait at the point of increased depth) and the answer is “passed upward” to the higher depth version. Like with loops, make sure your recursion has a way to return to the surface, instead of going deeper forever!

Python’s depth limit for recursion is exactly 1000 iterations before crashing, but this can be modified by changing a system variable.

What is the difference between Python modules and packages? Explain how to create, import, and use them in your Python programs.

A python module is simply a file that contains functional (as in, non-malformed) python code in text form, and has a .py extension. a module can be imported into another .py program by simply declaring import my-module. Running this code will make python look in the directory of the run directory for a my-module.py file, and if found, allow its variables and functions to be referable from within the program that it was imported to. You can also have python search in any locations listed in the PYTHONPATH variable.

Note that to actually access these variables or functions, you must use dot notation to indicate that the variable/function exists within the imported module, like so: my-module.print_username()

A package is a method of organizing several different modules that are designed to be used together, or are in some way related, and functions very similar to a simple folder on a computer. A package can be defined by creating a computer folder, inserting your modules into it, and adding the metadata file __init.py__ to communicate to python that the folder __init.py__ is in should be treated as a package. __init.py__ itself can be left blank, but you can also fill it with python code that determines the exact ways in which the package is imported, for instance, excluding all but 4 specific modules.

Back To Main Page.