GNU Free Documentation License . .

Python

: ,
Python
Python logo.svg
:

:
-,
,
,
-

:

, MSIL, - Java

:

1990

():

:

.py, .pyw, .pyc, .pyo, .pyd[1]

:

2.7.3, 3.2.3 (9 2012, 10 2012[2])

:

,

:

CPython, Jython, IronPython, PyPy, Stackless

:

ABC, Modula-3, Lisp, Tcl Smalltalk, C, Java, Icon

:

Ruby, Boo, Groovy, ECMAScript, CoffeeScript

PSFL

:

www.python.org

Python, 19902005

Python (. python  , [ˈpaɪθ⟨ə⟩n]  ́, ́)[3]  . Python . .

Python , , -, , -.   , , , , . , ( ).

Python CPython, [4]. Python Software Foundation License, , [5]. JVM ( ), MSIL ( ), LLVM . PyPy , .

Python  , ( / ) . Python ANSI, ISO , CPython.

[]

Python , «The Zen of Python» (« », « »)[6]. Ÿ import this ( ). .

:

«
  • , .
  • , .
  • , .
  • , .
  • , .
  • , .
  • .
  • , .
  • .
  • .
  • .
  • , .
  • , , .
  • , [7].
  • , .
  • , .
  • .
  • , , .
  • ! !
»

[]

Python 1980- [8] CWI . Amoeba , Python , ABC ( , ). 1991 alt.sources[9]. Python - .

.py

. 1970- « ». , ,   KDE Microsoft Windows python.org ( 2.5) .

, Python. , , PEP (Python Enhancement Proposal)  Python[10].

3 2008 [11], , Python 3000 ( Python 3.0, Py3k). Python 3000 ( ) . (Python 3.x 2.x).

[] Python

, Python :

Python (, - ) .

[]

Python   . Microsoft Windows, UNIX ( FreeBSD Linux), Plan 9, Mac OS Mac OS X, iPhone OS 2.0 , Palm OS, OS/2, Amiga, AS/400 OS/390, Windows Mobile, Symbian Android[14].

. , 2.6 Windows 95, Windows 98 Windows ME[15]. Python  Python 2.3 ( ).

, , Python (, Microsoft COM/DCOM). , Java  Jython, , Java, Java . Microsoft .NET,   IronPython Python.Net.

[]

Python , . « » « ». : , , Unicode-, , , . Python (tuples), , ( ) , 2.4, . , , , , .

(class), (, C). ( ) . .

. int, long, complex . , , , . . , , ,   . , , . [16], , .

[]

, , .

[]

. :

  • if (). else (). , elif (. else if).
  • while () for (). break continue .
  • class.
  • , def. return () ,   yield ().
  • try  except  else try  finally ( 2.5, finally, except else ).
  • pass . .

( ), begin/end, , , . «» «» . , . .[   324 ]

[]

. , , .

( printf() ), , :

>>> print (", %s!" % "")
, !

Python .   :

1 <= a < 10 and 1 <= b < 20

, (or and) : , , . : , «» (or) , . , , . 2.5 :

(a < b) and "" or "  "

, , ( ):

"" + ''  """ """  u"-"
True or False            #  
3.14                     #    
012 + 0xA                #       
1 + 2j                   #  
[1, 2, "a"]              # 
(1, 2, "a")              # 
{'a': 1, 'b': 'B'}       # 
lambda x: x**2           #  

( ) Python . , , . . s[N:M] , N M . .

[]

() , . ( import keyword; print keyword.kwlist) . , , [17].

( ): , .

( ). « », .

[]

Python pydoc. , ,   docstring (.). , [18], IDE (, Eclipse).

, doctest (.) .

[]

Python 2.3, , ASCII, , :

# -*- coding: utf-8 -*-

, , Unicode-.

[]

[]

, , , (REPL). , , , , .

:

>>> 2 ** 100                           #  2   100
1267650600228229401496703205376L
>>> from math import *                 #   
>>> sin(pi * 0.5)                      #      
1.0
>>> help(sorted)                       #    sorted
Help on built-in function sorted in module __builtin__:
sorted(...)
   sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list

pdb ( help()). , , .

, IPython[19] bpython[20].

[] -

Python - . , , - .

:

  1. .
  2. , .
  3. ( ).
  4. (   ).   .
  5. , : , , .
  6. (, is, '.', '=' ).
  7. ( ).
  8. ( , ,  . .).
  9. ( , len(), , , , )
  10. ( , , .)
  11. .
  12. , .
  13. , .

[]

Python , :

  • ( , , )
  • (, )

[]

( ) , . , ZIP-. : , «» , (extension modules), . , «» pickle : cPickle. ,   . import. , . reload().

[]

Python . , .

, pythonic style, Python, PyRO, PLY, Cherry, Django ., .

[]

Python try, except, else, finally, raise, . :

try:
    #  ,    
    raise Exception("message")  # Exception,       (  ),
                                #    ,    
except ( 1,  2, ), :
    #    ,        
    # ( 1,  2, )    
    #   .
    #      .
except ( 3,  4, ), :
    #   except  
    raise  #   "" ;   -   
except:
    #     ,     except
else:
    #   ,     .
finally:
    #     ,   
    #  except  else

else, except finally Python 2.5. sys.exc_info(). , Python   traceback.

, Python ( ) . Python (10- « Python»  « ») « ».

with (, Python 2.5).

[]

. for , . , , . . . itertools .

[]

  , : (. : ). . . : .

-, . ( next(), for ) yield return.

Python 2.4   , . , :

>>> sum(i for i in xrange(1, 100) if i % 2 != 0)
2500

1 99.

2.5, Python : send() throw().

[]

Python 2.5   with contextlib. .: .

, , return: , ,  . . , , .

[]

2.4, Python . . [21] ( ) ( ). @ , . :

def myWonderfulMethod():
    return " "
myWonderfulMethod = staticmethod(myWonderfulMethod)

:

@staticmethod
def myWonderfulMethod():
    return " "

, , . - .

2.6 , .

[]

Python , .

, , .

def getClass():
    return dict
class D(getClass()):
    pass
d = D()

, :

>>> class X(object): pass

>>> y = X()
>>> y.wrongMethod() #    
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'X' object has no attribute 'wrongMethod'
>>> X.wrongMethod = lambda self : 'im here' #  
>>> y.wrongMethod() #          __dict__ ,
'im here' #  wrongMethod    

[]

[]

Python « ».

. , , HTTP- , , XML  . . - . , , , , , , - .

[]

, ; , Win32 Win32 API, COM , Visual Basic Delphi. Python (, , , , ,  . .).

Python DB-API 2 : PostgreSQL, Oracle, Sybase, Firebird (Interbase), Informix, Microsoft SQL Server, MySQL SQLite. Windows ADO (ADOdb). mxODBC ODBC Windows UNIX eGenix[22]. ORM (SQLObject, SQLAlchemy, Dejavu, Django), - (Django, Pylons).

NumPy , . SciPy NumPy (   BLAS, level 1-3 LAPACK; ). Numarray[23] .

WSGI[24]  - (Python Web Server Gateway Interface).

Python C API C++. SWIG C/C++ . (C/C++/Fortran)-Python (SWIG, pyste[25], SIP[26], pyfort[27]), API (boost::python[28], CXX[29] .). ctypes /DLL, . , /C++ Python, « » (pyinline[30], weave[31]).

Python . Python Java, C/C++, Ocaml. Python- CORBA, XML-RPC, SOAP, COM.

Cython Python Pyrex (.) , . Cython Python-, .

Shedskin Python ++ . 0.22 Shedskin .

Python . , , Python .

[]

tkinter Tcl/Tk .

, GUI   wxPython[32], wxWidgets, PyGTK Gtk, PyQt PySide Qt . , , , .

, , Pygame. : , . pygame OpenGL PyOpenGL[33], - OpenGL. PyOgre[34], Ogre  - 3D-. , pythonOCC[35], 3D- OpenCascade[36].

Python Imaging Library.

[]

« Python» , Python .

[]

Python ( profile), . , , timeit. , :

from timeit import Timer
 
tmp = "Python 3.2.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32."
 
def case1():  # .    
    s = ""
    for i in range(10000):
        s += tmp
 
def case2():  # .      join
    s = []
    for i in range(10000):
        s.append(tmp)
    s = "".join(s)
 
def case3():  # .     join
    return "".join([tmp for i in range(10000)])
 
def case4():  # .     join
    return "".join(tmp for i in range(10000))
 
for v in range(1,5):
    print (Timer("func()","from __main__ import case%s as func" % v).timeit(200))

, . ( ) ( , ,  . .). .

:

  • , .
  • , .
  • , . , sort().
  • . .

Python :

  • , , , . , , , .
  • , . , , .
  • psyco , .
  • , , , , Pyrex.

Pychecker[37] (, ,  . .). . Pylint[38] , .

[]

Python Perl Ruby. . Perl, Python (). Ruby, Python .

Scheme Icon.

Python Java-[39].

, Python , .

[]

. Python[40].

[]

, , , , JIT-,   [41]. , Python [41]. , , Python, 2-4 , C++ Java[41]. - ( .pyc .pyo) , , , Perl. , JIT- psyco[42], ( ). psyco .

Python, () . PyPy, LLVM; Parrot. , LLVM , Java, [43].

/ (. ) .

Python , Tcl, Forth, LISP Lua, . , Python [44].

[]

, -. , , . , Python « ». , AttributeError . .

, , typecheck[45] method signature checking decorators[46]. Python 3, , .[47]

Python . Python , , , , , , . Python .

len = lambda x : x.__len__() #   

[48][49], Python.

, PEAK[50][51] . Python3000[52], overloading-lib[53].

[]

Ruby , Python , , int, str, float, list , , , Python . . ( ) Python- - -API.

[] (GIL)

GIL (Global Interpreter Lock)  , CPython, Stackless PyPy, Jython IronPython. Python - . , , (  . .). , ( 100) GIL, . , Python Python , (GIL , -, /   , , ). , - / [54]. GIL , , Python , , GIL, , , GIL . python-safethread[55]  CPython GIL ( , 60-65 % CPython).

.   . ( ) [56]. ( , Windows  Unix , ).

    . / .

Python , , , processing[57], threading, . 2.6, processing multiprocessing. , / Python, parallelpython[58], Pypar[59], pympi[60] . GIL , , NumPy/SciPy, Python . IronPython Jython, Python.

3.2 GIL[61][62]. :

  • ,   GIL 100 . , . 5 ;
  •   ( MacOS X) , GIL : . ;
  • ( , GIL ).

[]

CPython , Python. :

Jython  Python, JVM . Java-.[63]

PyS60[44]  Nokia Series 60.

IronPython  Python .NET Framework Mono. Python MSIL, .NET [64].

Stackless  Python. , CPython. .

Python for .NET[65]  Python .NET. IronPython Python MSIL, , C#. .NET- Python .

PyPy  Python, Python. . PyPy CPython Stackless, Psyco, AST « » . Python (, LLVM, Javascript, .NET 0.9.9). 0.9.0, , , ( 23 CPython JIT 0.9.9). JIT .

python-safethread[55]  CPython GIL, Python . .

Unladen Swallow  Google , CPython JIT- LLVM. Python[66], Unladen Swallow CPython 3.3. PEP-3146 Unladen Swallow Google, .[67]

tinypy[68]  Python. CPython .

[]

Python Enhancement Proposal («PEP»)  , Python, , . PEP . PEP BDFL.

[]

Python 2.x Python 3.x , 2.x 3.x. PEP 3000 .

Python 3.0 2.x. Python 2.x Python 3.0. Python Python 2.x Python 3.0 . , «2to3» , . PEP 3000 2.x, Python 3.x «2to3». , Python 2.x.

[]

, 3.0:[69][70]

  • (, ).
  • unicode .
  • « » « ». .
  • - ( io), .
  • , ( 2.6).
  • .
  • {k: v for k, v in a_dict} {el1, el2, el3} ( ).
  • print . , , . Python 2.6 from __future__ import print_function.
  • reduce ( map filter) functools ( reduce ).
  • , 2.x , : , , , , exec  . .
  • .
  • .
  • . , , (a, *rest, b) = range(5). , def foo(a, (b, c)) .

[] / Python

Python , . :

RPython[71]  PyPy Python . RPython /  C, JavaScript, Lisp, .NET[72], LLVM. RPython PyPy.

Pyrex[73]  Python, , RPython. Pyrex . , .

Cython[74]  Pyrex.

pyastra[75]  Python PIC .

Shedskin  Python ++.

[]

Python  . : . Python , . Python .[76]

Python NumPy, SciPy MatPlotLib Matlab, IDL .[77][78]

[] .

[]

  1. Python:
    .py  .
    .pyw  Windows, .
    .pyc  -.
    .pyo  -.
    .pyd  DLL ( Windows).
  2. Python.
  3. , , « ». .: http://docs.python.org/faq/general#why-is-it-called-python, .
  4. About Python
  5. Python 2.5 license
  6. PEP 20  The Zen of Python
  7. The Making of Python
  8. http://svn.python.org/view/*checkout*/python/trunk/Misc/HISTORY
  9. Index of Python Enhancement Proposals (PEPs)
  10. Python 3.0 Release
  11. 1 2 Foreword for «Programming Python» (1st ed.)
  12. The Making of Python
  13. Python on Android  (.). www.damonkohler.com. 28 2011. 19 2009.
  14. Port-Specific Changes: Windows  (.). Python v2.6.1 documentation. Whats New in Python 2.6. Python Software Foundation. 28 2011. 11 2008.
  15. Are tuples more efficient than lists in Python?  Stack Overflow
  16. 2.3.2. Reserved classes of identifiers. Python documentation (18 2009). 28 2011.
  17. Python : doc-
  18. http://ipython.scipy.org/
  19. bpython interpreter
  20. PEP318
  21. eGenix.com  Professional Python Software, Skills and Services
  22. numarray Home Page
  23. PEP333
  24. Pyste Documentation
  25. http://www.riverbankcomputing.co.uk/sip/
  26. http://pyfortran.sourceforge.net/
  27. Boost.Python
  28. PyCXX: Write Python Extensions in C
  29. PyInline: Mix Other Languages directly Inline with your Python
  30. Weave
  31. wxPython
  32. PyOpenGL  The Python OpenGL Binding
  33. PyOgre : Ogre enci
  34. pythonOCC, 3D CAD/CAE/PLM development framework for the Python programming language
  35. Open CASCADE Technology, 3D modeling & numerical simulation
  36. PyChecker: a python source code checking tool
  37. pylint (analyzes Python source code looking for bugs and signs of poor quality.) (Logilab.org)
  38. zephyrfalcon.org :: labs :: 10 Python pitfalls
  39. 1 2 3 Python / C++ GNU g++. Computer Language Benchmarks Game.  ???. 28 2011. 1 2009.
  40. Psyco (.)  JIT- Python, 3-10
  41. unladen-swallow. A faster implementation of Python. code.google.  «Goals: Produce a version of Python at least 5x faster than CPython»  28 2011. 22 2009.
  42. 1 2 Python for S60  OpenSource
  43. Typechecking module for Python
  44. Method signature checking decorators " Python recipes " ActiveState Code
  45. PEP-3107
  46. http://alpha.sec.ru/~aiv/python/overload/
  47. http://python.com.ua/doc/overload.html
  48. FrontPage  The PEAK Developers' Center
  49. PEAK-Rules
  50. PEP-3124
  51. overloading-lib, python
  52. Python 3000 FAQ
  53. 1 2 python-safethread  Project Hosting on Google Code
  54. perlthrtut  perldoc.perl.org
  55. Python Package Index : processing 0.52
  56. Parallel Python  Home
  57. http://datamining.anu.edu.au/~ole/pypar/
  58. pyMPI.sourceforge.net: Putting the py in MPI
  59. Whats New In Python 3.2  Python v3.2b2 documentation
  60. [Python-Dev] Reworking the GIL
  61. The Jython Project
  62. IronPython
  63. Python for .NET
  64. PEP 3146  Merging Unladen Swallow into CPython
  65. http://qinsb.blogspot.com/2011/03/unladen-swallow-retrospective.html
  66. tinypy :: home
  67. Whats New In Python 3.0  Python v3.0.1 documentation
  68. Overview  Python v3.0.1 documentation
  69. PyPy[coding-guide]
  70. PyPy[carbonpython]
  71. Pyrex
  72. Cython: C-Extensions for Python
  73. Pyastra: python assembler translator
  74. Python Success Stories
  75. Python vs Matlab Python for scientists blog
  76. IDL vs. Python AstroBetter Tips and Tricks for Professional Astronomers

[]

  • David Beazley, Guido Van Rossum. Python: Essential Reference.  New Riders Publishing, 1999.
  • Martin C. Brown. Python: The Complete Reference. McGraw-Hill Professional Publishing, 2001
  • Wesley J. Chun. Core Python Programming. Prentice Hall PTR, 2000
  • Alan Gauld. Learn to Program Using Python: A Tutorial for Hobbyists, Self-Starters, and Those Who Want to Learn the Art of Programming. Addison-Wesley Professional, 2001
  • John E. Grayson. Python and Tkinter Programming. Manning Publications Company, 1999
  • Rashi Gupta. Making use of Python. Wiley, 2002
  • Mark Hammond, Andy Robinson. Python Programming on Win32. OReilly, 2000
  • Christopher A. Jones, Fred L. Drake. Python & XML. OReilly & Associates, 2001
  • Ivan Van Laningham. Teach Yourself Python in 24 Hours. Sams, 2000
  • Amos Latteier, Michel Pelletier. The Zope Book. New Riders Publishing, 2001
  • Frederik Lundh. Python Standard Library. OReilly & Associates, 2001
  • A. Sweigart. Invent Your Own Computer Games with Python.  20082010.  436 .  ISBN 978-0-9821060-1-3

[]