Python itself has a lot of very useful modules built into it, and as soon as the installation is complete, these modules can be used immediately.
For example, we built the SYS module as a hello module:
#!/usr/bin/env python
#-*-coding:utf-8-*-
' a test module '
__author__ = ' Michael Liao '
import sys
def Test ():
args = sys.argv
if Len (args) ==1:
print ' Hello, world! '
Elif len (args) ==2:
print ' Hello,%s! '% args[1]
else:
print ' Too many '
If __name__== ' __main__ ':
Test ()
Lines 1th and 2nd are standard comments, and the 1th line comment allows the hello.py file to run directly on the Unix/linux/mac, and the 2nd line comment indicates that the. py file itself uses standard UTF-8 encoding;
Line 4th is a string that represents the document annotation for the module, and the first string of any module code is treated as a document annotation for the module;
Line 6th Use the __author__ variable to write the author, so that when you open the source code, others will be able to visit your name;
This is the Python module of the standard file template, of course, you can delete all do not write, but, according to the standard work certainly correct.
The back start is the real code section.
As you may notice, the first step in using the SYS module is to import the module:
After the SYS module is imported, we have the variable sys pointing to the module, and using the SYS variable to access all the functions of the SYS module.
The SYS module has a argv variable that stores all the parameters of the command line with the list. ARGV has at least one element because the first argument is always the name of the. py file, for example:
The sys.argv you get from running Python hello.py is [' hello.py '];
The sys.argv that runs Python hello.py Michael is [' hello.py ', ' Michael].
Finally, notice the two lines of code:
If __name__== ' __main__ ':
Test ()
When we run the Hello module file at the command line, the Python interpreter __name__ a special variable to __main__, and if the Hello module is imported elsewhere, the If judgment fails, so This if test allows a module to execute some extra code when it runs through the command line, most commonly by running tests.
We can run hello.py with the command line to see the effect:
$ python hello.py
Hello, world!
$ python hello.py Michael
Hello, michael!
If you start the Python interactive environment, and then import the Hello module:
$ python
python 2.7.5 (default, Aug 2013, 00:04:04)
[GCC 4.2.1 compatible Apple LLVM 5.0 (clang-500.0.68)] on D Arwin
Type ' help ', ' copyright ', ' credits ' or ' license ' for the more information.
>>> Import Hello
>>>
When importing, Hello was not printed, word!, because the test () function was not executed.
When you call Hello.test (), you can print Hello, word!:
>>> hello.test ()
Hello, world!
Alias
When you import a module, you can also use aliases so that you can select the most appropriate module at run time based on the current environment. For example, the Python standard library typically provides Stringio and Cstringio two libraries, the interfaces and functions of the two libraries are the same, but Cstringio is written in C, faster, so you will often see the wording:
Try:
import Cstringio as Stringio
except Importerror: # import failure captures Importerror
import Stringio
This allows you to import Cstringio first. If some platforms do not provide Cstringio, you can also demote the use of Stringio. When importing Cstringio, use import ... as ... Alias Stringio is specified, so subsequent code references Stringio to work correctly.
There are libraries like Simplejson, which are independent third-party libraries before Python 2.6, built from 2.6, so there's a way to do this:
Try:
import JSON # python >= 2.6
except Importerror:
import Simplejson as JSON # python <= 2.5
Since Python is a dynamic language, the function signature is identical to the interface, so no matter which module is imported, subsequent code will work correctly.
Scope
In a module, we may define many functions and variables, but some functions and variables we want to use for others, and some functions and variables we want to use only within the module. In Python, this is done by the _ prefix.
Normal functions and variable names are public, and can be directly referenced, such as: ABC,X123,PI;
A variable like __xxx__ is a special variable that can be referenced directly, but for a special purpose, such as the __author__,__name__ above is a special variable, the document annotation defined by the module can also be accessed with a special variable __doc__. Our own variables generally do not use this variable name;
Functions or variables like _xxx and __xxx are Non-public (private) and should not be directly referenced, such as _ABC,__ABC;
The reason we say that private functions and variables "should not" be directly referenced, rather than "cannot" be directly referenced, is because Python does not have a way to completely restrict access to private functions or variables, but from a programming custom should not refer to private functions or variables.
Private functions or variables should not be referenced by others, so what is the use of them? Take a look at the example:
def _private_1 (name): Return
' Hello,%s '% name
def _private_2 (name): Return
' Hi,%s '% name
def greeting (name):
If Len (name) > 3: Return
_private_1 (name)
else: return
_private_2 (name)
We expose the greeting () function in the module, and hide the internal logic in private function, so that calling the greeting () function does not concern the details of the private function inside, which is also a very useful code encapsulation and abstract method, namely:
Functions that do not need to be referenced externally are all defined as private, and only functions that need to be referenced externally are defined as public.