1.Introduction¶
This reference manual describes the Python programming language. It is notintended as a tutorial.
While I am trying to be as precise as possible, I chose to use English ratherthan formal specifications for everything except syntax and lexical analysis.This should make the document more understandable to the average reader, butwill leave room for ambiguities. Consequently, if you were coming from Mars andtried to re-implement Python from this document alone, you might have to guessthings and in fact you would probably end up implementing quite a differentlanguage. On the other hand, if you are using Python and wonder what the preciserules about a particular area of the language are, you should definitely be ableto find them here. If you would like to see a more formal definition of thelanguage, maybe you could volunteer your time — or invent a cloning machine:-).
It is dangerous to add too many implementation details to a language referencedocument — the implementation may change, and other implementations of thesame language may work differently. On the other hand, CPython is the onePython implementation in widespread use (although alternate implementationscontinue to gain support), and its particular quirks are sometimes worth beingmentioned, especially where the implementation imposes additional limitations.Therefore, you’ll find short “implementation notes” sprinkled throughout thetext.
Every Python implementation comes with a number of built-in and standardmodules. These are documented inThe Python Standard Library. A few built-in modulesare mentioned when they interact in a significant way with the languagedefinition.
1.1.Alternate Implementations¶
Though there is one Python implementation which is by far the most popular,there are some alternate implementations which are of particular interest todifferent audiences.
Known implementations include:
- CPython
This is the original and most-maintained implementation of Python, written in C.New language features generally appear here first.
- Jython
Python implemented in Java. This implementation can be used as a scriptinglanguage for Java applications, or can be used to create applications using theJava class libraries. It is also often used to create tests for Java libraries.More information can be found atthe Jython website.
- Python for .NET
This implementation actually uses the CPython implementation, but is a managed.NET application and makes .NET libraries available. It was created by BrianLloyd. For more information, see thePython for .NET home page.
- IronPython
An alternate Python for .NET. Unlike Python.NET, this is a complete Pythonimplementation that generates IL, and compiles Python code directly to .NETassemblies. It was created by Jim Hugunin, the original creator of Jython. Formore information, seethe IronPython website.
- PyPy
An implementation of Python written completely in Python. It supports severaladvanced features not found in other implementations like stackless supportand a Just in Time compiler. One of the goals of the project is to encourageexperimentation with the language itself by making it easier to modify theinterpreter (since it is written in Python). Additional information isavailable onthe PyPy project’s home page.
Each of these implementations varies in some way from the language as documentedin this manual, or introduces specific information beyond what’s covered in thestandard Python documentation. Please refer to the implementation-specificdocumentation to determine what else you need to know about the specificimplementation you’re using.
1.2.Notation¶
The descriptions of lexical analysis and syntax use a modifiedBackus–Naur form (BNF) grammarnotation. This uses the following style of definition:
name ::=lc_letter
(lc_letter
| "_")*lc_letter ::= "a"..."z"
The first line says that aname
is anlc_letter
followed by a sequenceof zero or morelc_letter
s and underscores. Anlc_letter
in turn isany of the single characters'a'
through'z'
. (This rule is actuallyadhered to for the names defined in lexical and grammar rules in this document.)
Each rule begins with a name (which is the name defined by the rule) and::=
. A vertical bar (|
) is used to separate alternatives; it is theleast binding operator in this notation. A star (*
) means zero or morerepetitions of the preceding item; likewise, a plus (+
) means one or morerepetitions, and a phrase enclosed in square brackets ([]
) means zero orone occurrences (in other words, the enclosed phrase is optional). The*
and+
operators bind as tightly as possible; parentheses are used forgrouping. Literal strings are enclosed in quotes. White space is onlymeaningful to separate tokens. Rules are normally contained on a single line;rules with many alternatives may be formatted alternatively with each line afterthe first beginning with a vertical bar.
In lexical definitions (as the example above), two more conventions are used:Two literal characters separated by three dots mean a choice of any singlecharacter in the given (inclusive) range of ASCII characters. A phrase betweenangular brackets (<...>
) gives an informal description of the symboldefined; e.g., this could be used to describe the notion of ‘control character’if needed.
Even though the notation used is almost the same, there is a big differencebetween the meaning of lexical and syntactic definitions: a lexical definitionoperates on the individual characters of the input source, while a syntaxdefinition operates on the stream of tokens generated by the lexical analysis.All uses of BNF in the next chapter (“Lexical Analysis”) are lexicaldefinitions; uses in subsequent chapters are syntactic definitions.