|
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??ж??壩??
????????????????鶨???е?????????????
>>> 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”???????????????????????????滻??????????????????????????Щ???а?????????·???????е???????????Щ???????ú?????????????????????????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???????????????????????乲???
???????????
??-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()????????????????鶨?壬???????????????????洢?б??
>>> 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??鵼?????????????????????????from ... import?????????????????Sound.Effects?????????????__all__???????????????
??????????????????????????????????import * ??????????飬??????????????ζ????????䶮????????????????????????????????????????????鱻????????????????????????????????
?????from
Package import
specific_submodule??д?????????????????????????????????е????????飬????????????????д????
???????侭????????????á????磬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