|
Python ??? |
|
????? 5. ????? ????? Python ??? ??? 7. ????????
?????
????????Python???????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
?????????????Python??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
????????Python??????????????????????????????????.py?????????????????????????????????????????__name__?????????????????????????????????????????????fibo.py???????????????????
# Fibonacci numbers module def fib(n): # write Fibonacci series up to n a, b = 0, 1 while b < n: print b, a, b = b, a+b def fib2(n): # return Fibonacci series up to n result = [] a, b = 0, 1 while b < n: result.append(b) a, b = b, a+b return result
???????Python???????????????????????
>>> import fibo??????????????fibo????????????????????????????????????fibo???????????????????????????????????
>>> fibo.fib(1000) 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 >>> fibo.fib2(100) [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] >>> fibo.__name__ 'fibo'
??????????????????????????????????????????
>>> fib = fibo.fib >>> fib(500) 1 1 2 3 5 8 13 21 34 55 89 144 233 377
????????????????????????????????????????????????????????????????????6.1
???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????modname.itemname??
?????????import?????????????????import?????????????????????????????????????????????????????????????????????
import??????????????????????????????????????????????????
>>> from fibo import fib, fib2 >>> fib(500) 1 1 2 3 5 8 13 21 34 55 89 144 233 377
???????????????????????????????fibo??????
????????????????E????????????????
>>> from fibo import * >>> fib(500) 1 1 2 3 5 8 13 21 34 55 89 144 233 377
?????????????????????????_?????????????
?????????spam???????????????????????????????spam.py???????????????????PYTHONPATH?????????????????????????????PATH??????????PYTHONPATH??????????????????????????????????????????UNIX??????? .:/usr/local/lib/python??
??????????????sys.path??????????????????????????????????????????????????????????PATHPATH??????????????????Python???????????programs????????????“programer”???????????????????????????I????????????????????????????????????????????????????????????????????????????????????????Python????????????????????????????????????????????? 6.2??“??????”??????????????
??????????????????????????????????????????????????????spam.py????????????spam.pyc??????????????spam?????“????”??``byte-compiled'' ?????????????????????spam.pyc???????spam.py????????????spam.pyc????????????????.pyc???????????
?????????????spam.pyc??????ʦ????????spam.py??????????????????????spam.pyc??????????????????????????spam.pyc???????????????????????spam.pyc????????????????????????Python???????????????????????k???
???????????
??-O????????Python????????????????????????????.pyo???????????????????????????????????????assert????????-O????????????????????.pyc??????????.py?????????????????
??Python?????????????? -O ??????-OO???????????????????????????????????????????????????????.pyo??????????????????????__doc__??????????????????????????????????????????????????????????????
????.pyc?????.pyo???????????????.py???????????.pyc??.pyo?????????????????????????
??????????????????????????????y??????.pyc??.pyo????????????????????????????????????????????????????????????????????????????????????????????????????????????.pyc???.pyo?????
???????????????????spam??????????????????spam.pyc?????????spam.pyo??????? -O ????????????spam.py???????????????????????????????Python?????
compileall??? ????????????????????.pyc????????????-O????????.pyo???????
Python??????????????????????????????????
Python
??????
?????????“??????”???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????amoeba????????Amoeba??????????????????????????????sys ??????????????????Python????????????
sys.ps1
?? sys.ps2
??????????????????????????????
>>> import sys >>> sys.ps1 '>>> ' >>> sys.ps2 '... ' >>> sys.ps1 = 'C> ' C> print 'Yuck!' Yuck! C>
?????????????????????????????????????????These two variables are only defined if the interpreter is in interactive mode. ????
????sys.path
?????????????????????????????????????PYTHONPATH??????????PYTHONPATH???????????????????????????????????????????????????????
>>> import sys >>> sys.path.append('/ufs/guido/lib/python')
??????dir()????????????????E???????????????????????
>>> import fibo, sys >>> dir(fibo) ['__name__', 'fib', 'fib2'] >>> dir(sys) ['__displayhook__', '__doc__', '__excepthook__', '__name__', '__stderr__', '__stdin__', '__stdout__', '_getframe', 'api_version', 'argv', 'builtin_module_names', 'byteorder', 'callstats', 'copyright', 'displayhook', 'exc_clear', 'exc_info', 'exc_type', 'excepthook', 'exec_prefix', 'executable', 'exit', 'getdefaultencoding', 'getdlopenflags', 'getrecursionlimit', 'getrefcount', 'hexversion', 'maxint', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'ps1', 'ps2', 'setcheckinterval', 'setdlopenflags', 'setprofile', 'setrecursionlimit', 'settrace', 'stderr', 'stdin', 'stdout', 'version', 'version_info', 'warnoptions']
????????????dir()?????????????????????
>>> a = [1, 2, 3, 4, 5] >>> import fibo, sys >>> fib = fibo.fib >>> dir() ['__name__', 'a', 'fib', 'fibo', 'sys']
??????????????????????????????????????????
dir()?????????????????????????????????????????????????__buildin__???
>>> import __builtin__ >>> dir(__builtin__) ['ArithmeticError', 'AssertionError', 'AttributeError', 'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False', 'FloatingPointError', 'IOError', 'ImportError', 'IndentationError', 'IndexError', 'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'NameError', 'None', 'NotImplemented', 'NotImplementedError', 'OSError', 'OverflowError', 'OverflowWarning', 'PendingDeprecationWarning', 'ReferenceError', 'RuntimeError', 'RuntimeWarning', 'StandardError', 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 'True', 'TypeError', 'UnboundLocalError', 'UnicodeError', 'UserWarning', 'ValueError', 'Warning', 'ZeroDivisionError', '__debug__', '__doc__', '__import__', '__name__', 'abs', 'apply', 'bool', 'buffer', 'callable', 'chr', 'classmethod', 'cmp', 'coerce', 'compile', 'complex', 'copyright', 'credits', 'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'execfile', 'exit', 'file', 'filter', 'float', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'int', 'intern', 'isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals', 'long', 'map', 'max', 'min', 'object', 'oct', 'open', 'ord', 'pow', 'property', 'quit', 'range', 'raw_input', 'reduce', 'reload', 'repr', 'round', 'setattr', 'slice', 'staticmethod', 'str', 'string', 'sum', 'super', 'tuple', 'type', 'unichr', 'unicode', 'vars', 'xrange', 'zip']
????????????“????????”?????????????????????A.B????????????“A”????????“B”??????????????????????????????????????????????????????????????????????NunPy??Python Imaging Library??????????????????????????????????
??????????????????????????“??”????????????????????????????????????????????????????????????????????????.wav??.aiff??.au?????????????????????????????????????????????????????????????? ????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
Sound/ Top-level package __init__.py Initialize the sound package Formats/ Subpackage for file format conversions __init__.py wavread.py wavwrite.py aiffread.py aiffwrite.py auread.py auwrite.py ... Effects/ Subpackage for sound effects __init__.py echo.py surround.py reverse.py ... Filters/ Subpackage for filters __init__.py equalizer.py vocoder.py karaoke.py ...
??????????Python???sys.path????????????????????????
??????????__init__.py
??????????????Python??????????????????????????????“string”?????????????????????????????????????????????????????????__init__.py
??????????????????????????????????????????????????????
__all__
????????????????????
????????????????????????
import Sound.Effects.echo???????????Sound.Effects.echo?????????????????????????????
Sound.Effects.echo.echofilter(input, output, delay=0.7, atten=4)???????????????????????
from Sound.Effects import echo???????????echo????????????????????????????????????????????????????????
echo.echofilter(input, output, delay=0.7, atten=4)???????????????????????????????
from Sound.Effects.echo import echofilter???????????????echo????????????????????????? echofilter() ??????
echofilter(input, output, delay=0.7, atten=4)
???????????? from
package import item
??????????????????item????????????????????????????????????????????????????????????????????import
???????????????????????????????????????????????????????????????????????????????
ImportError
????
???????????import
item.subitem.subsubitem
????????????????????????????????????????????????????????????????????????????
??????????from
Sound.Effects import
*?????????????????????????????????????????????????????????????????????????Mac
?? Windows
??????????????????????????????????????????????????????????????????ECHO.PY???????????????echo??Echo??ECHO???????Windows
95???????????????????????????????????????????????
DOS
8+3?????????????????????????????????????????
?????????????????????????????????????????????import???????????????????????from packae import * ??????????__init__.py??????????????__all__????????????????????????????????????????????????????????????????????????????import * ??????????????????????????????????????????import *???????Sounds/Effects/__init__.py ???????????????????
__all__ = ["echo", "surround", "reverse"]
?????? from
Sound.Effects import * ?????Sound
??????????????????????????
???????? __all__
??from
Sound.Effects import * ?????????Sound.Effects??????????????Effects
???????????????????????????????
Sound.Effects
?????????????
__init__.py????????????????????????????????????????????__init__.py????????????????????????????????????????????????import?????????????????????????????
import Sound.Effects.echo import Sound.Effects.surround from Sound.Effects import *
???????????echo??surround??y?????????????????????????from ... import?????????????????Sound.Effects?????????????__all__???????????????
??????????????????????????????????import * ????????????????????????????????????????????????????????????????????????????????????????????????????????????
?????from
Package import
specific_submodule???????????????????????????????????????????????????????????????
???????B????????????????surround
???????????echo
?????????????????????????????import???????????????????????????????????????surround
module ??????????import
echo ???? from
echo import echofilter???????????????????????????import?????????????????????????
????????????????????????????Sound??????????????????????????????????????????????????????????????
????Sound.Filters.vocoder
????????Sound.Effects
????echosa???????????from
Sound.Effects import echo??
????????????????????? __path__?? ?????__init__.py????????????????????????????????????????????????????????????????????????????
?????????????????????????????????????
|
Python ??? |
|
????? 5. ????? ????? Python ??? ??? 7. ????????
Release 2.3, documentation updated on July 29, 2003.
See About this document... for information on suggesting changes. ?????, @ssv 97av