if __name__ == "__main__":

Modules are objects, and all modules have a built-in attribute __name__. A module's __name__ depends on how you're using the module. If you import the module, then __name__ is the module's filename, without a directory path or file extension. But you can also run the module directly as a standalone program, in which case __name__ will be a special default value, __main__.

 

Knowing this, you can design a test suite for your module within the module itself by putting it in this if statement. When you run the module directly, __name__ is __main__, so the test suite executes. When you import the module, __name__ is something else, so the test suite is ignored. This makes it easier to develop and debug new modules before integrating them into a larger program.

相关文章:

  • 2021-06-06
  • 2021-08-01
  • 2021-05-17
  • 2021-12-05
  • 2021-06-04
  • 2021-12-25
  • 2021-08-13
  • 2021-06-21
猜你喜欢
  • 2021-07-11
  • 2022-12-23
  • 2021-05-19
  • 2022-01-04
  • 2021-06-02
  • 2021-10-21
相关资源
相似解决方案