An example analysis of Python module structure and Layout operation method

  • 2020-06-15 09:18:35
  • OfStack

This paper illustrates the Python module structure and layout operation method. To share for your reference, specific as follows:


#coding=utf8
# The starting line 
#!/usr/bin/env python
# Module documentation 
'''''
 reasonable Module Layout: 
(1)  The starting line (Unix)
(2)  Module documentation 
(3)  Module import 
(4)  Variable definitions 
(5)  The class definition 
(6)  The function definitions 
(7)  The main program 
-----------------------------
(1)  The starting line (Unix)
 Usually only in the class Unix The start line is used only in the environment, where a start line can be entered to execute the script. 
(2)  Module documentation 
 Briefly introduce the function of the module and the meaning of important global variables module.__doc__ Access these contents. 
(3)  Module import 
 All modules required to import the code of the current module; Each module is imported only 1 Times; 
 The module import code inside the function will not be executed unless the function is executing. 
(4)  Variable definitions 
 The variables defined here are global variables and all functions in this module can be used directly. 
 Try to replace global variables with local ones, which is easier to maintain, improves performance and saves memory. 
(5)  The class definition 
 All classes need to be defined here. When the module is imported class The statement is executed and the class is defined. 
 The document variable of class is class.__doc__
(6)  The function definitions 
 The function defined here can be passed module.function() Is accessed externally when the module is imported def The statement will be executed, 
 The function is defined, and the document variable of the function is function.__doc__
(7)  The main program 
 This code is executed whether the module is imported by another module or executed directly as a script. 
 Typically, there isn't much functional code, but instead calls different functions based on the pattern of execution. 
'''
# The import module 
import sys
import time
from scrapy.utils import job
# Define variables 
flag=1
# Kind of fixing 
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)
# The function definitions 
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()
# The main program 
# If the module is to be imported, __name__ Is the module name 
# If the module is to be executed directly, __name__ The value of '__main__'
if __name__=='__main__':
  test()

More Python related content interested readers to view this site project: "Python introduction and advanced tutorial", "Python string skills summary", "Python list (list) skills summary", "Python coding skills summary", "Python data structure and algorithm tutorial", "Python function using techniques" and "Python file and directory skills summary"

I hope this article has been helpful in Python programming.


Related articles: