reading-notes

TDD

it’s just one of many techniques that can help you to develop better software, It provides a safety net of tests that ensure your code works as expected and can be easily maintained and improved.

The key principles of TDD in Python include:

  1. Write a failing test first.

  2. Write only enough code to make the test pass.

  3. Once the test is passing, you should refactor the code to improve its design, readability, and performance. 

  4. To ensures that the code is always tested, repeat the process after refactoring, you should write another failing test and repeat the process.

Main Gate

The if __name__ == '__main__' statement in Python is used to determine whether a script is being run as the main program or being imported as a module by another script. This helps to ensure that certain code is executed only when the script is run as the main program and not when it is being used as a module by another script. In simpler terms, it allows you to write code that can be both used as a standalone program and as a module can be imported into other programs.

Cases for including this conditional in the code include:

  1. Testing: by putting the test code inside the if name == 'main': statement, you can ensure that it only runs when the module is run as the main program.

  2. Command-line interface: If you’re creating a command-line interface for your module, you can put the code to parse command-line arguments and run the program inside the if name == 'main': statement. This way, the code will only run when the module is run as the main program from the command line.

  3. Example usage: You may want to include an example usage of your module in the same file. By putting this code inside the if name == 'main': statement, you can ensure that it only runs when the module is run as the main program, making it easier for users to understand how to use your module.

# Recursion is a programming technique that involves a function calling itself in order to solve a problem. In Python, recursion is used to solve problems that can be broken down into smaller subproblems, each of which can be solved by calling the same function recursively. Recursion is a powerful concept that allows programmers to write elegant and concise code to solve complex problems.

Advantages of using recursion:

  1. A complicated function can be split down into smaller sub-problems utilizing recursion.
  2. Sequence creation is simpler through recursion than utilizing any nested iteration.
  3. Recursive functions render the code look simple and effective.

Disadvantages of using recursion:

  1. A lot of memory and time is taken through recursive calls which makes it expensive for use.
  2. Recursive functions are challenging to debug.
  3. The reasoning behind recursion can sometimes be tough to think through.
Syntax:

 def func(): <--
              |
              | (recursive call)
              |
    func() ---- 

Recursive functions can be less efficient than iterative solutions, as each recursive call adds a new stack frame to the call stack.

The difference between Python modules and packages

  1. Think of a module as a collection of tools or functions that you can use in your Python programs. It’s like having a box of tools that you can use to build different things. Each tool in the box is designed to do a specific job. Similarly, each module is designed to perform a specific task or provide a particular functionality. 1.1 To create a module, you need to write some Python code and save it in a file with a .py extension. This file will contain the tools or functions that you want to use in your other Python programs. Once you’ve created your module, you can import it into your other programs and use its tools or functions as needed. This way, you don’t have to rewrite the same code over and over again in different programs. You can simply import the module and use its pre-built functionality.

  2. A package, on the other hand, is a collection of modules that are organized into a directory hierarchy. A package can contain sub-packages, which are just nested packages within the main package. A package must have an init.py file, which can contain initialization code that is executed when the package is imported. To create a package, you need to create a directory with an init.py file and one or more Python modules.