What is a module
(1) Modules are logically organized in the form of Python code
(2) When the code is quite large, it is best to divide the code into organized pieces of code, provided that they interact with each other
(3) There is a connection between these snippets, which may be a class that contains data members and methods, or a set of related but independent operation functions
(4) Module name can not be confused, alphanumeric underline composition, the first letter can not be a number
Import Module
(1) Import the module using import, after the module is imported, the program will automatically generate the PYc bytecode file to improve performance
(2) module properties are called through the method call of the module name. Properties, and can be imported separately if only some of the properties in the module are required
(3) The module has a __name__ special attribute, when the module file is executed directly, the value of __name__ is "__main__" when
When the module is imported by another file, the value of the __name__ is the name of the module
>>> Import sys //Import SYS module
>>> import os,string //Importing two modules at a time, OS, String Module
>>> string.digits //Call method in Module
' 0123456789 '
>>> from random import randint //import only Randint function in random module
>>> randint (1,10) //Using the Randint function
2 //Output results
Module loading
(1) A module is loaded only once, no matter how many times it is imported
(2) Load only once to prevent multiple import code from being executed multiple times
(3) If two files are imported to each other to prevent infinite loading
(4) When the module is loaded, the top-level code executes automatically, so it is good programming practice to just put the function in the top layer of the module.
If __name__ = "__main__":
Execute statement//can be a test statement, the function of the test function
This avoids the "execute statement" when the module is imported, and the basic writing program uses this structure
>>> Import String
>>> import tab //Import tab module, so you can implement auto-completion
>>> string.lowercase //Call string module, resulting in lowercase letters
' abcdefghijklmnopqrstuvwxyz '
>>> string.uppercase //Call string module to generate uppercase letters
' ABCDEFGHIJKLMNOPQRSTUVWXYZ '
>>> string.letters //Call string module to generate letters
' abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz '
>>> string.digits //Call the digits property of the string module, producing the number
' 0123456789 '
>>> string.__file__ // You can view the source code of the string module
'/usr/lib64/python2.6/string.pyc //full path to remove the end C path
Script instance
A random password to generate a user-specified number of digits
#!/usr/bin/env python
Import string//Imports string module for generating strings
Import Random//Imports random module, used to randomly take a single character
Import SYS//importing SYS for positional parameters
num = Int (sys.argv[1])//Pass position parameter and convert character type to integer
All_chs = string.digits + string.letters//assigns a string of letters and numbers to All_chs
def mkpasswd (num)://Define a function that generates a password
PWD = ""//variable assignment null value
For I in range (num)://Loop execution
PWD + = Random.choice (All_chs)//directly stitching randomly generated strings
return pwd//return pwd
if __name__ = = "__main__"://For testing the code, you can also use this program output the password to generate
Print mkpasswd (num)
SYS.ARGV This module is used to store the program name and Parameters argv (0) is the program name, argv (1) is the first parameter
This article is from the "Court of the Odd Tree" blog, please be sure to keep this source http://zhangdl.blog.51cto.com/11050780/1828100
Writing: Introduction to the Python module & script to output user-specified number of digits password