PEP 236 – Back to the __future__ | peps.python.org
PEP 236 – Back to the __future__ PEP 236 – Back to the __future__ Author : Tim Peters <tim.peters at gmail.com> Status : Final Type : Standards Track Created : 26-Feb-2001 Python-Version : 2.1 Post-History : 26-Feb-2001 Table of Contents Motivation Intent Syntax Semantics Example Standard Module __future__.py Resolved Problem: Runtime Compilation Resolved Problem: Native Interactive Shells Resolved Problem: Simulated Interactive Shells Questions and Answers What about a “from __past__” version, to get back old behavior?
What about incompatibilities due to changes in the Python virtual machine? What about incompatibilities due to changes in Python’s C API? I want to wrap future_statements in try/except blocks, so I can use different code depending on which version of Python I’m running. Why can’t I? Going back to the nested_scopes example, what if release 2.2 comes along and I still haven’t changed my code? How can I keep the 2.1 behavior then?
Overloading import sucks. Why not introduce a new statement for this? Copyright References and Footnotes Motivation From time to time, Python makes an incompatible change to the advertised semantics of core language constructs, or changes their accidental (implementation-dependent) behavior in some way. While this is never done capriciously, and is always done with the aim of improving the language over the long term, over the short term it’s contentious and disrupting.
PEP 5 , Guidelines for Language Evolution suggests ways to ease the pain, and this PEP introduces some machinery in support of that. PEP 227 , Statically Nested Scopes is the first application, and will be used as an example here. Intent [Note: This is policy, and so should eventually move into PEP 5 ] When an incompatible change to core language syntax or semantics is being made: The release C that introduces the change does not change the syntax or semantics by default.
A future release R is identified in which the new syntax or semantics will be enforced. The mechanisms described in PEP 230 , Warning Framework are used to generate warnings, whenever possible, about constructs or operations whose meaning may [1] change in release R. The new future_statement (see below) can be explicitly included in a module M to request that the code in module M use the new syntax or semantics in the current release C.
So old code continues to work by default, for at least one release, although it may start to generate new warning messages. Migration to the new syntax or semantics can proceed during that time, using the future_statement to make modules containing it act as if the new syntax or semantics were already being enforced. Note that there is no need to involve the future_statement machinery in new features unless they can break existing code; fully backward- compatible additions can– and should –be introduced without a corresponding future_statement.
Syntax A future_statement is simply a from/import statement using the reserved module name __future__ : future_statement : "from" "__future__" "import" feature [ "as" name ] ( "," feature [ "as" name ]) * feature : identifier name : identifier In addition, all future_statements must appear near the top of the module. The only lines that can appear before a future_statement are: The module docstring (if any). Comments.
Blank lines. Other future_statements. Example: """This is a module docstring.""" # This is a comment, preceded by a blank line and followed by # a future_statement. from __future__ import nested_scopes from math import sin from __future__ import alabaster_weenoblobs # compile-time error! # That was an error because preceded by a non-future_statement. Semantics A future_statement is recognized and treated specially at compile time: changes to the semantics of core constructs are often implemented by generating different code.
It may even be the case that a new feature introduces new incompatible syntax (such as a new reserved word), in which case the compiler may need to parse the module differently. Such decisions cannot be pushed off until runtime. For any given release, the compiler knows which feature names have been defined, and raises a compile-time error if a future_statement contains a feature not known to it [2] . The direct runtime semantics are the same as for any import statement: there is a standard module __future__.py , described later, and it will be imported in the usual way at the time the future_statement is executed.
The interesting runtime semantics depend on the specific feature(s) “imported” by the future_statement(s) appearing in the module. Note that there is nothing special about the statement: import __future__ [ as name ] That is not a future_statement; it’s an ordinary import statement, with no special semantics or syntax restrictions. Example Consider this code, in file scope.py: x = 42 def f (): x = 666 def g (): print "x is" , x g () f () Under 2.0, it prints: x is 42 Nested scopes ( PEP 227 ) are being introduced in 2.1.
But under 2.1, it still prints: x is 42 and also generates a warning. In 2.2, and also in 2.1 if from __future__ import nested_scopes is included at the top of scope.py , it prints: x is 666 Standard Module __future__.py Lib/__future__.py is a real module, and serves three purposes: To avoid confusing existing tools that analyze import statements and expect to find the modules they’re importing. To ensure that future_statements run under releases prior to 2.1 at least yield runtime exceptions (the import of __future__ will fail, because there was no module of that name prior to 2.1).
To document when incompatible changes were introduced, and when they will be– or were –made mandatory. This is a form of executable documentation, and can be inspected programmatically via importing __future__ and examining its contents. Each statement in __future__.py is of the form: FeatureName = "_Feature(" OptionalRelease "," MandatoryRelease ")" where, normally, OptionalRelease < MandatoryRelease , and both are 5-tuples of the same form as sys.version_info : ( PY_MAJOR_VERSION , # the 2 in 2.1.0a3; an int PY_MINOR_VERSION , # the 1; an int PY_MICRO_VERSION , # the 0; an int PY_RELEASE_LEVEL , # "alpha", "beta", "candidate" or "final"; string PY_RELEASE_SERIAL # the 3; an int ) OptionalRelease records the first release in which: from __future__ import FeatureName was accepted.
In the case of MandatoryReleases that have not yet occurred, MandatoryRelease predicts the release in which the feature will become part of the language. Else MandatoryRelease records when the feature became part of the language; in releases at or after that, modules no longer need: from __future__ import FeatureName to use the feature in question, but may continue to use such imports. MandatoryRelease may also be None , meaning that a planned feature got dropped.
Instances of class _Feature have two corresponding methods, .getOptionalRelease() and .getMandatoryRelease() . No feature line will ever be deleted from __future__.py . Example line: nested_scopes = _Feature (( 2 , 1 , 0 , "beta" , 1 ), ( 2 , 2 , 0 , "final" , 0 )) This means that: from __future__ import nested_scopes will work in all releases at or after 2.1b1, and that nested_scopes are intended to be enforced starting in release 2.2.
Resolved Problem: Runtime Compilation Several Python features can compile code during a module’s runtime: The exec statement. The execfile() function. The compile() function. The eval() function. The input() function. Since a module M containing a future_statement naming feature F explicitly requests that the current release act like a future release with respect to F, any code compiled dynamically from text passed to one of these from within M should probably also use the new syntax or semantics associated with F.
The 2.1 release does behave this way. This isn’t always desired, though. For example, doctest.testmod(M) compiles examples taken from strings i
The rest of this page is on the live site.