Python module structure and layout Operation Method Instance analysis and python instance analysis
This article describes the Python module structure and layout operations. We will share this with you for your reference. The details are as follows:
# Coding = utf8 # start line #! /Usr/bin/env python # Module documentation ''' reasonable Module layout: (1) start line (Unix) (2) Module documentation (3) Module import (4) variable definition (5) class definition (6) Function Definition (7) main program ----------------------------- (1) the starting line (Unix) is generally used only in Unix-like environments, you can enter a script name to execute the script. (2) The module document briefly introduces the functions of the module and the meanings of important global variables. You can access these contents through module. _ doc _ outside the module. (3) The module imports all the modules required for the code of the current module. Each module only imports the code once. The module imports the code inside the function is not executed unless the function is being executed. (4) variables are defined as global variables. All functions in this module can be directly used. Use local variables instead of global variables as much as possible, which is not only easy to maintain, but also improves performance and saves memory. (5) class definition all classes need to be defined here. When a module is imported, the class statement is executed and the class is also defined. The document variable of the class is class. _ doc _ (6) the function defined here can be defined through module. function () is accessed externally. When a module is imported, the def statement is executed, and the function is defined. The variable in the function documentation is function. _ doc _ (7) the main program will execute this part of code whether it is imported by another module or directly executed as a script. Generally, there is not much functional code here, but different functions are called according to the execution mode. ''' # Import module import sysimport timefrom scrapy. utils import job # define the variable flag = 1 # class dingting class Person (object ): '''''person class set Person name, sex, age, job output the person information ''' def _ init _ (self): self. name = ''self. sex = ''self. age = 18 self. job = ''def setName (self, name): self. name = name def setSex (self, sex): self. sex = sex def setAge (self, age): self. age = age def setJob (self, job): self. job = job def outPut (self): print ''' name: % s sex: % s age: % d job: % s ''' % (self. name, self. sex, self. age, self. job) # function Definition def test (): '''''test function''' if flag: print ''' run test () '''person = person () Person. setName ("ewang") person. setAge (25) person. setSex ("famale") person. setJob ("big data testing") person. outPut () # Main Program # If the module is imported, the value of __name _ is the module name # If the module is directly executed, __name _ is '_ main _' if _ name __= = '_ main _': test ()
For more Python-related content, refer to this topic: getting started with Python and advanced classic tutorial, Python string operation skills summary, and Python list) operation Skills summary, Python coding skills summary, Python data structure and algorithm tutorial, Python function usage skills summary, and Python file and directory operation skills Summary
I hope this article will help you with Python programming.