blob: ac8b553ecc940cb66adf89263dea1895622e8f5c [file] [log] [blame]
Jeremy Hylton324cc6e2001-03-23 15:29:54 +00001\chapter{Appendix: Future statements and nested scopes}
2
3The semantics of Python's static scoping will change in version 2.2 to
4support resolution of unbound local names in enclosing functions'
5namespaces. The new semantics will be available in Python 2.1 through
6the use of a future statement. This appendix documents these two
7features for Python 2.1; it will be removed in Python 2.2 and the
8features will be documented in the main sections of this manual.
9
10\section{Future statements}
11\indexii{future}{statement}
12
13A \dfn{future statement} is a directive to the compiler that a
14particular module should be compiled using syntax or semantics that
15will be available in a specified future release of Python. The future
16statement is intended to ease migration to future versions of Python
17that introduce incompatible changes to the language. It allows use of
18the new features on a per-module basis before the release in which the
19feature becomes standard.
20
21\begin{verbatim}
22future_statement: "from" "__future__" "import" feature ["as" name]
23 ("," feature ["as" name])*
24
25feature: identifier
26name: identifier
27\end{verbatim}
28
29A future statement must appear near the top of the module. The only
30lines that can appear before a future statement are:
31
32\begin{itemize}
33
34\item the module docstring (if any),
35\item comments,
36\item blank lines, and
37\item other future statements.
38
39\end{itemize}
40
41The only feature recognized by Python 2.1 is \samp{nested_scopes}.
42
43A future statement is recognized and treated specially at compile time:
44Changes to the semantics of core constructs are often implemented by
45generating different code. It may even be the case that a new feature
46introduces new incompatible syntax (such as a new reserved word), in
47which case the compiler may need to parse the module differently. Such
48decisions cannot be pushed off until runtime.
49
50For any given release, the compiler knows which feature names have been
51defined, and raises a compile-time error if a future statement contains
52a feature not known to it.
53
54The direct runtime semantics are the same as for any import statement:
55there is a standard module \file{__future__.py}, described later, and
56it will be imported in the usual way at the time the future statement
57is executed.
58
59The interesting runtime semantics depend on the specific feature
60enabled by the future statement.
61
62Note that there is nothing special about the statement:
63
64\begin{verbatim}
65import __future__ [as name]
66\end{verbatim}
67
68That is not a future statement; it's an ordinary import statement, with
69no special semantics or syntax restrictions.
70
71Code compiled by an exec statement or calls to the builtin functions
72\function{compile} and \function{execfile} that occur in a module M
73containing a future statement will use the new syntax or semantics
74associated with the future statement.
75
76A future statement typed at an interactive interpreter prompt will
77take effect for the rest of the interpreter session. If an
78interpreter is started with the \emph{-i} option, is passed a
79script name to execute, and the script includes a future statement, it
80will be in effect in the interactive session started after the script
81is executed.
82
83\section{\module{__future__} ---
84 Future statement definitions}
85
86\declaremodule{standard}{__future__}
87\modulesynopsis{Future statement definitions}
88
89\file{__future__.py} is a real module, and serves three purposes:
90
91\begin{itemize}
92
93\item To avoid confusing existing tools that analyze import statements
94 and expect to find the modules they're importing.
95
96\item To ensure that future_statements run under releases prior to 2.1
97 at least yield runtime exceptions (the import of
98 \code{__future__} will fail, because there was no module of
99 that name prior to 2.1).
100
101\item To document when incompatible changes were introduced, and when they
102 will be --- or were --- made mandatory. This is a form of executable
103 documentation, and can be inspected programatically via importing
104 \code{__future__} and examining its contents.
105
106\end{itemize}
107
108Each statment in \file{__future__.py} is of the form:
109
110\begin{verbatim}
111FeatureName = "_Feature(" OptionalRelease "," MandatoryRelease ")"
112\end{verbatim}
113
114where, normally, OptionalRelease < MandatoryRelease, and both are
1155-tuples of the same form as \code{sys.version_info}:
116
117\begin{verbatim}
118 (PY_MAJOR_VERSION, # the 2 in 2.1.0a3; an int
119 PY_MINOR_VERSION, # the 1; an int
120 PY_MICRO_VERSION, # the 0; an int
121 PY_RELEASE_LEVEL, # "alpha", "beta", "candidate" or "final"; string
122 PY_RELEASE_SERIAL # the 3; an int
123 )
124\end{verbatim}
125
126OptionalRelease records the first release in which the feature was
127accepted.
128
129In the case of MandatoryReleases that have not yet occurred,
130MandatoryRelease predicts the release in which the feature will become
131part of the language.
132
133Else MandatoryRelease records when the feature became part of the
134language; in releases at or after that, modules no longer need a
135future statement to use the feature in question, but may continue to
136use such imports.
137
138MandatoryRelease may also be None, meaning that a planned feature got
139dropped.
140
141Instances of class \class{_Feature} have two corresponding methods,
142\member{getOptionalRelease()} and \member{getMandatoryRelease()}.
143
144No feature line will ever be deleted from \file{__future__.py}.
145
146\section{Nested scopes}
147\indexii{nested}{scopes}
148
149Nested scopes are left as an exercise for the reader.