[Python-Dev] PEP 236: an alternative to the __future__ syntax

Martin v. Loewis martin@loewis.home.cs.tu-berlin.de
Wed, 28 Feb 2001 23:19:13 +0100


PEP 236 states that the intention of the proposed feature is to allow
modules "to request that the code in module M use the new syntax or
semantics in the current release C".

It achieves this by introducing a new statement, the
future_statement. This looks like an import statement, but isn't. The
PEP author admits that 'overloading "import" does suck'. I agree (not
surprisingly, since Tim added this QA item after we discussed it in
email).

It also says "But if we introduce a new keyword, that in itself would
break old code". Here I disagree, and I propose patch 404997 as an
alternative
(https://sourceforge.net/tracker/index.php?func=detail&aid=404997&group_id=5470&atid=305470)

<specification section="Alternative Solution">
In essence, with that patch, you would write

directive nested_scopes

instead of

from __future__ import nested_scopes

This looks like as it would add a new keyword directive, and thus
break code that uses "directive" as an identifier, but it doesn't.  In
this release, "directive" is only a keyword if it is the first keyword
in a file (i.e. potentially after a doc string, but not after any
other keyword). So

class directive:
  def __init__(self, directive):
    self.directive = directive

continues to work as it did in previous releases (it does not even
produce a warning, but could if desired). Only when you do

directive nested_scopes
directive braces
class directive:
  def __init__(self, directive):
    self.directive = directive

you get a syntax error, since "directive" is then a keyword in that
module.

The directive statement has a similar syntax to the C #pragma
"statement", in that each directive has a name and an optional
argument. The choice of the keyword "directive" is somewhat arbitrary;
it was deliberately not "pragma", since that implies an
implementation-defined semantics (which directive does not have).

In terms of backwards compatibility, it behaves similar to "from
__future__ import ...": older releases will give a SyntaxError for the
directive syntax (instead of an ImportError, which a __future__ import
will give). "Unknown" directives will also give a SyntaxError, similar
to the ImportError from the __future__ import.
</specification>

Please let me know what you think. If you think this should be written
down in a PEP, I'd request that the specification above is added into 
PEP 236.

Regards,
Martin