blob: 6234716563f9956a04dbce10ee4728b2cf30a707 [file] [log] [blame]
Fred Drake61c77281998-07-28 19:34:22 +00001\chapter{Introduction\label{introduction}}
Fred Drakef6669171998-05-06 19:52:49 +00002
3This reference manual describes the Python programming language.
4It is not intended as a tutorial.
5
6While I am trying to be as precise as possible, I chose to use English
7rather than formal specifications for everything except syntax and
8lexical analysis. This should make the document more understandable
9to the average reader, but will leave room for ambiguities.
10Consequently, if you were coming from Mars and tried to re-implement
11Python from this document alone, you might have to guess things and in
12fact you would probably end up implementing quite a different language.
13On the other hand, if you are using
14Python and wonder what the precise rules about a particular area of
15the language are, you should definitely be able to find them here.
Thomas Woutersf9b526d2000-07-16 19:05:38 +000016If you would like to see a more formal definition of the language,
Guido van Rossum0bd37951998-06-15 16:27:37 +000017maybe you could volunteer your time --- or invent a cloning machine
18:-).
Fred Drakef6669171998-05-06 19:52:49 +000019
20It is dangerous to add too many implementation details to a language
21reference document --- the implementation may change, and other
22implementations of the same language may work differently. On the
Guido van Rossum0bd37951998-06-15 16:27:37 +000023other hand, there is currently only one Python implementation in
Fred Drake21c82542005-07-20 04:33:01 +000024widespread use (although alternate implementations exist), and
Fred Drakef6669171998-05-06 19:52:49 +000025its particular quirks are sometimes worth being mentioned, especially
26where the implementation imposes additional limitations. Therefore,
27you'll find short ``implementation notes'' sprinkled throughout the
28text.
29
30Every Python implementation comes with a number of built-in and
31standard modules. These are not documented here, but in the separate
Fred Drakee15eb351999-11-10 16:13:25 +000032\citetitle[../lib/lib.html]{Python Library Reference} document. A few
33built-in modules are mentioned when they interact in a significant way
34with the language definition.
Fred Drakef6669171998-05-06 19:52:49 +000035
Fred Drake2829f1c2001-06-23 05:27:20 +000036
Fred Drake21c82542005-07-20 04:33:01 +000037\section{Alternate Implementations\label{implementations}}
38
39Though there is one Python implementation which is by far the most
40popular, there are some alternate implementations which are of
41particular interest to different audiences.
42
43Known implementations include:
44
45\begin{itemize}
46\item[CPython]
47This is the original and most-maintained implementation of Python,
48written in C. New language features generally appear here first.
49
50\item[Jython]
51Python implemented in Java. This implementation can be used as a
52scripting language for Java applications, or can be used to create
53applications using the Java class libraries. It is also often used to
54create tests for Java libraries. More information can be found at
55\ulink{the Jython website}{http://www.jython.org/}.
56
57\item[Python for .NET]
58This implementation actually uses the CPython implementation, but is a
59managed .NET application and makes .NET libraries available. This was
60created by Brian Lloyd. For more information, see the \ulink{Python
61for .NET home page}{http://www.zope.org/Members/Brian/PythonNet}.
62
63\item[IronPython]
64An alternate Python for\ .NET. Unlike Python.NET, this is a complete
65Python implementation that generates IL, and compiles Python code
66directly to\ .NET assemblies. It was created by Jim Hugunin, the
67original creator of Jython. For more information, see \ulink{the
68IronPython website}{http://workspaces.gotdotnet.com/ironpython}.
69
70\item[PyPy]
71An implementation of Python written in Python; even the bytecode
72interpreter is written in Python. This is executed using CPython as
73the underlying interpreter. One of the goals of the project is to
74encourage experimentation with the language itself by making it easier
75to modify the interpreter (since it is written in Python). Additional
76information is available on \ulink{the PyPy project's home
77page}{http://codespeak.net/pypy/}.
78\end{itemize}
79
80Each of these implementations varies in some way from the language as
81documented in this manual, or introduces specific information beyond
82what's covered in the standard Python documentation. Please refer to
83the implementation-specific documentation to determine what else you
84need to know about the specific implementation you're using.
85
86
Fred Drake61c77281998-07-28 19:34:22 +000087\section{Notation\label{notation}}
Fred Drakef6669171998-05-06 19:52:49 +000088
89The descriptions of lexical analysis and syntax use a modified BNF
90grammar notation. This uses the following style of definition:
91\index{BNF}
92\index{grammar}
93\index{syntax}
94\index{notation}
95
Guido van Rossumd8faa362007-04-27 19:54:29 +000096\begin{productionlist}[*]
Andrew M. Kuchling6a15c5d2005-06-02 13:50:19 +000097 \production{name}{\token{lc_letter} (\token{lc_letter} | "_")*}
98 \production{lc_letter}{"a"..."z"}
99\end{productionlist}
Fred Drakef6669171998-05-06 19:52:49 +0000100
Fred Drake5c07d9b1998-05-14 19:37:06 +0000101The first line says that a \code{name} is an \code{lc_letter} followed by
102a sequence of zero or more \code{lc_letter}s and underscores. An
103\code{lc_letter} in turn is any of the single characters \character{a}
104through \character{z}. (This rule is actually adhered to for the
105names defined in lexical and grammar rules in this document.)
Fred Drakef6669171998-05-06 19:52:49 +0000106
107Each rule begins with a name (which is the name defined by the rule)
Andrew M. Kuchling6a15c5d2005-06-02 13:50:19 +0000108and \code{::=}. A vertical bar (\code{|}) is used to separate
Fred Drakef6669171998-05-06 19:52:49 +0000109alternatives; it is the least binding operator in this notation. A
Fred Drake5c07d9b1998-05-14 19:37:06 +0000110star (\code{*}) means zero or more repetitions of the preceding item;
111likewise, a plus (\code{+}) means one or more repetitions, and a
112phrase enclosed in square brackets (\code{[ ]}) means zero or one
Fred Drakef6669171998-05-06 19:52:49 +0000113occurrences (in other words, the enclosed phrase is optional). The
Fred Drake5c07d9b1998-05-14 19:37:06 +0000114\code{*} and \code{+} operators bind as tightly as possible;
Fred Drakef6669171998-05-06 19:52:49 +0000115parentheses are used for grouping. Literal strings are enclosed in
116quotes. White space is only meaningful to separate tokens.
117Rules are normally contained on a single line; rules with many
118alternatives may be formatted alternatively with each line after the
119first beginning with a vertical bar.
120
121In lexical definitions (as the example above), two more conventions
122are used: Two literal characters separated by three dots mean a choice
123of any single character in the given (inclusive) range of \ASCII{}
Fred Drake5c07d9b1998-05-14 19:37:06 +0000124characters. A phrase between angular brackets (\code{<...>}) gives an
Guido van Rossum7c0240f1998-07-24 15:36:43 +0000125informal description of the symbol defined; e.g., this could be used
Fred Drakef6669171998-05-06 19:52:49 +0000126to describe the notion of `control character' if needed.
127\index{lexical definitions}
Fred Drakec37b65e2001-11-28 07:26:15 +0000128\index{ASCII@\ASCII}
Fred Drakef6669171998-05-06 19:52:49 +0000129
130Even though the notation used is almost the same, there is a big
131difference between the meaning of lexical and syntactic definitions:
132a lexical definition operates on the individual characters of the
133input source, while a syntax definition operates on the stream of
134tokens generated by the lexical analysis. All uses of BNF in the next
135chapter (``Lexical Analysis'') are lexical definitions; uses in
136subsequent chapters are syntactic definitions.