reading-notes

Reading & Video Review Notes

In Tests We Trust - TDD w/ Python

If name equals main

Recursion

What on Earth is Recursion

  1. What are the key principles of Test-Driven Development (TDD) in Python, and how do they contribute to the overall quality of code?
    • craft the software design first
    • code is more reliable
    • tests can then be run after any changes
    • hard to easy strategy
      • this methodology contributes to the overall quality of the code by ensuring that changes can be tested and verified every step of the way. The initial time investment may be difficult but the assurances provided by the tests throughout the process are worth their digital weight in bitcoin.
  2. Explain the purpose of the if name == ‘main’: statement in Python scripts. What are some use cases for including this conditional in your code?
    • controls the use of certain code executions when the scripts are run.
    • __name__ is set to the name of the script
    • used primarily to define functinos or classes that are run directly
    • helps keep code organized and prevents functions or classes from being accidentally imported into other scripts
    • Uses
      • test the functionality of code without having to run it directly
      • create reusable modules that can be imported to other scripts
  3. Describe the concept of recursion in Python.
    • the process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function.
    • solves a particular problem by calling a copy of itself and solving smaller subproblems of the original problems
    • recursion is the best solution for a task that can be defined by its similar subtasks.
  4. 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 a file that contains python code
      • can be imported to other python programs
      • resuable code
      • code can be organized into smaller, more manageable chunks
    • a python package is a collection of modules
      • can be imported into other python programs
      • allows organization into larger, more complex applications
    • Create module
      • a file with a .py extenstion
      • Import
        • use the import keyword
        • Use
          • refer to it by name my_module.py
    • Create package
      • create a directory w/ .py init.py
      • Import
        • importlib module
        • Use
          • refer to it by fullly qualified name