This is the 15th day of my participation in the August More Text Challenge. For details, see:August is more challenging

1. Evolution of modules and packages

With the rapid development of The Times, technology is more and more integrated into every corner of our lives. From the ability to buy goods around the world at your fingertips, to the development of autonomous vehicle technology, the procedures for achieving these functions are becoming more and more complex, so let’s see how modul has evolved

  • At first, the program implements only a few single-threaded functions

  • As more and more statements come out of functions

    The implementation of the same function of multiple statements encapsulated into the function, unified management and call

  • With functions and variables comes classes and objects

    The data and behavior of objects of the same type, namely variables and functions, are managed and invoked in a unified way

  • With classes comes modules

    Put all the functions and classes that implement similar functionality into one module

  • With modules comes packages

    Put together modules that do similar things

The above module and package evolution is born step by step with the idea of “quantitative change causes qualitative change” and “birds of a feather flock together”.

  1. A Python program consists of modules. A module corresponds to a Python source file, usually with a.py suffix
  2. Modules consist of statements. Allows Python programs to execute in the order in which the statements in a module are executed
  3. Statements are the building blocks of a Python program and are used to create objects, assign values to variables, call functions, control statements, and so on

2. Why study modules

The module corresponds to the Python source (.py file). A module can define variables, functions, classes, and plain statements.

Modular programming will decompose a task into multiple modules, each module is like a building block, easy to use and build repeatedly later

Advantages of modular programming:

  1. It is convenient to decompose a task into multiple modules to realize team collaborative development and complete large-scale programs
  2. Realize code reuse. After a module is implemented, it can be called repeatedly
  3. Improved maintainability

3. Module division (Standard Library)

  • Modules are divided into standard modules and user-defined modules
  • Python standard library provides operating system functions, network communication, text processing, file processing, data operation and other basic functions
  • Python provides a large number of third-party modules, similar to the standard library, for artificial intelligence, big data, image recognition, and more

4. Modularize the programming process

  • API design, function description, generally in the requirements analysis phase of pre-research
  • The code implements the functionality described in the API
  • Write the test code in the pattern and eliminate the global code
  • Use private functions to implement module functions that are not called by external clients

5. API overview of the module

Application Programming Interface (API) is used to describe the functions and classes provided in a module.

In modular programming, you first design the API for the pattern (that is, the description of the functionality to be implemented), and then you start coding to implement the functionality described in the API. Finally, import this module in another module for invocation.

  • API view mode, currently there are four ways

    1. You can view the module API by using help(module name).

      (1) Import modules in general use

      (2) View through the help function

      import time
      
      help(time)
      Copy the code
    2. IDE view import library after long press Ctrl (MAC Command) key, quickly jump to the library file (.py)

    1. Query in the Python API documentation.

      (1) Go to the docs subdirectory in the Python installation directory

      (2) Double-click the CHM document and enter “math” through the index to query the corresponding API content

    1. Online documentation for API related introduction and methods

6. Module creation

Let’s implement the custom module using modular programming and import the custom module

import hellojuejin

hellojuejin.say_hey("ANNE")
Copy the code

We can call the module’s __doc__ attribute to see the module documentation

import hellojuejin

print(hellojuejin.__doc__)

Copy the code

conclusion

In this installment, we will learn the idea of modular programming, and practice and master the process of module creation and import

In practice, the idea of modularity helps us practice a more effective approach to our projects

See you next time ~ღ(´ · ᴗ · ‘) 🌹🌹🌹