1. module name
fibo.__name__
# file fibo.py
def f(x):
return x*x
import fibo
fibo.f(10) # module name then function name
fl = fibo.f # to make it call like locally
from fibo import f
from fibo import * # import all except for name start with ‘_‘
2. module search path
a. current path
b. pythonpath
c. path
# not find or not set pythonpath search installment path(/usr/local/lib/python)
3. while pyc time not the same as py time, drop it(byte compiled)
pyc is standalone for plant
-O pyo, optimize the code(delete allert)
-OO pyo, delete __doc__ string
pyc or pyo file doesn‘t mean run faster than py, but load faster
it will not create a pyc or pyo when run command line
compileall can create .pyc for all module in some path
4. import sys
sys.ps1( >>>) sys.ps2(...) those two variable only defined if the interpreter is interactive mode
5. change sys path
import sys
sys.path.append(/usr/guido/lib/python)
6. dir()
import flib,sys
dir(flib)
dir() # show current definition include function, variable, module
it will not show inner function and variable and can reset it from __builtin__
7. package
Sound/
__init__.py
Formats/
__init__.py
wavread.py
wavwrite.py
...
Effects/
__init__.py
echo.py
...
Filter/
equalizer.py
it is necessary exit a __init__.py file
import Sound.Effects.echo
Sound.Effects.echo.echofilter(input,output,delay=0.7,atten=4)
from Sound.Effects import echo
echo.echofilter(input,output,delay=0.7,atten=4)
from Sound.Effects.echo import echofilter
echofilter(input,output,delay=0.7,atten=4)
from package import item (item can be subpackage,module,function,class,variable),if can‘t find this item then importError exception
import package.subpackage.item(item must be subpackage or module in this situation)
from Package import specific_subpackage
__init__.py have a statement __all__ = [‘reverse‘,‘echo‘,‘surround‘]
8.especially variable ( __path__ ) before __init__.py execute this variable get a initial list, use to extend a package‘s module set.
原文地址:http://blog.csdn.net/zcliatb/article/details/42171179