Python module File structure code details, python module code details

Source: Internet
Author: User

Python module File structure code details, python module code details

This article focuses on the file structure of the Python module.

Python file structure (full text)
  #/usr/bin/env python  "this is a test module"  import sys  import os  debug = True  class FooClass (object):    "Foo class"    pass  def test():    "test function"    foo = FooClass()    if debugL      print 'ran test()'  if __name__ == '__main__'    test()
File structure (branch Introduction)

(1) starting line (specifying the version of Python used)

  #/usr/bin/env python

(2) module document (document string, if it is expanded with three single quotes)

  "this is a test module"

(3) module Import

  import sys  import os

(4) (global) variable definition

  debug = True

(5) class definition (if any)

Class FooClass (object): "Foo class" pass (6) function Definition (if any) def test (): "test function" foo = FooClass () if debugL print 'ran test ()'

7) Main Program

  if __name__ == '__main__'    test()

Description

Main Program
  • This part of code is executed no matter whether the current module is imported or directly run as a script.

Note: All modules have the ability to execute code

  • The high-level Python Statement (without indentation) is executed when the module is imported.
  • Proper practice: Except for the code that really needs to be executed, all functional code is built through the function. Therefore:

Write a large number of top-level executable code only in the main program module
Only a small number of top-level Execution Code should exist in the imported module.

_ Name _ indicates how the module should be loaded (so as to implement self-check of the module)

  • If the module is imported, the value __name _ is the module name.
  • If the module is executed directly, the value of __name _ is _ main __
Summary

The above is all the details about the file structure code of the Python module. I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.