2000-08-15 09:42:44 -04:00
|
|
|
|
PEP: 221
|
|
|
|
|
Title: Import As
|
|
|
|
|
Version: $Revision$
|
2000-08-23 01:57:47 -04:00
|
|
|
|
Author: thomas@xs4all.net (Thomas Wouters)
|
2000-08-20 06:49:45 -04:00
|
|
|
|
Status: Accepted
|
2000-08-23 08:09:07 -04:00
|
|
|
|
Type: Standards Track
|
2000-08-23 01:57:47 -04:00
|
|
|
|
Python-Version: 2.0
|
|
|
|
|
Created: 15-Aug-2000
|
|
|
|
|
Post-History:
|
2000-08-15 09:42:44 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Introduction
|
|
|
|
|
|
2000-08-23 01:57:47 -04:00
|
|
|
|
This PEP describes the `import as' proposal for Python 2.0. This
|
|
|
|
|
PEP tracks the status and ownership of this feature. It contains
|
|
|
|
|
a description of the feature and outlines changes necessary to
|
|
|
|
|
support the feature. The CVS revision history of this file
|
2000-08-15 09:42:44 -04:00
|
|
|
|
contains the definitive historical record.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Rationale
|
|
|
|
|
|
2000-08-23 01:57:47 -04:00
|
|
|
|
This PEP proposes an extention of Python syntax regarding the
|
|
|
|
|
`import' and `from <module> import' statements. These statements
|
|
|
|
|
load in a module, and either bind that module to a local name, or
|
|
|
|
|
binds objects from that module to a local name. However, it is
|
|
|
|
|
sometimes desirable to bind those objects to a different name, for
|
|
|
|
|
instance to avoid name clashes. This can currently be achieved
|
|
|
|
|
using the following idiom:
|
2000-08-15 09:42:44 -04:00
|
|
|
|
|
2000-08-20 06:49:45 -04:00
|
|
|
|
import os
|
|
|
|
|
real_os = os
|
|
|
|
|
del os
|
2000-08-15 09:42:44 -04:00
|
|
|
|
|
2000-08-23 01:57:47 -04:00
|
|
|
|
And similarly for the `from ... import' statement:
|
2000-08-15 09:42:44 -04:00
|
|
|
|
|
2000-08-20 06:49:45 -04:00
|
|
|
|
from os import fdopen, exit, stat
|
|
|
|
|
os_fdopen = fdopen
|
|
|
|
|
os_stat = stat
|
|
|
|
|
del fdopen, stat
|
2000-08-15 09:42:44 -04:00
|
|
|
|
|
|
|
|
|
The proposed syntax change would add an optional `as' clause to
|
|
|
|
|
both these statements, as follows:
|
|
|
|
|
|
2000-08-20 06:49:45 -04:00
|
|
|
|
import os as real_os
|
|
|
|
|
from os import fdopen as os_fdopen, exit, stat as os_stat
|
2000-08-15 09:42:44 -04:00
|
|
|
|
|
|
|
|
|
The `as' name is not intended to be a keyword, and some trickery
|
2000-08-23 01:57:47 -04:00
|
|
|
|
has to be used to convince the CPython parser it isn't one. For
|
2000-08-15 09:42:44 -04:00
|
|
|
|
more advanced parsers/tokenizers, however, this should not be a
|
|
|
|
|
problem.
|
2000-08-23 08:09:07 -04:00
|
|
|
|
|
|
|
|
|
A slightly special case exists for importing sub-modules. The
|
|
|
|
|
statement
|
|
|
|
|
|
|
|
|
|
import os.path
|
|
|
|
|
|
|
|
|
|
stores the module `os' locally as `os', so that the imported
|
|
|
|
|
submodule `path' is accessible as `os.path'. As a result,
|
2000-08-20 06:49:45 -04:00
|
|
|
|
|
2000-08-23 08:09:07 -04:00
|
|
|
|
import os.path as p
|
|
|
|
|
|
|
|
|
|
should store `os.path', not `os', in `p'. The current
|
|
|
|
|
implementation does not yet support this.
|
2000-08-15 09:42:44 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Implementation details
|
|
|
|
|
|
2000-08-20 06:49:45 -04:00
|
|
|
|
This PEP has been accepted, and the suggested code change has been
|
2000-08-23 01:57:47 -04:00
|
|
|
|
checked in. The patch can still be found in the SourceForge patch
|
|
|
|
|
manager[1]. Currently, a NAME field is used in the grammar rather
|
|
|
|
|
than a bare string, to avoid the keyword issue. It introduces a
|
2000-08-20 06:49:45 -04:00
|
|
|
|
new bytecode, IMPORT_STAR, which performs the `from module import
|
|
|
|
|
*' behaviour, and changes the behaviour of the IMPORT_FROM
|
|
|
|
|
bytecode so that it loads the requested name (which is always a
|
|
|
|
|
single name) onto the stack, to be subsequently stored by a STORE
|
|
|
|
|
opcode.
|
|
|
|
|
|
2000-08-15 09:42:44 -04:00
|
|
|
|
The special case of `from module import *' remains a special case,
|
2000-08-20 06:49:45 -04:00
|
|
|
|
in that it cannot accomodate an `as' clause, and that no STORE
|
|
|
|
|
opcodes are generated; the objects imported are loaded directly
|
|
|
|
|
into the local namespace.
|
2000-08-15 09:42:44 -04:00
|
|
|
|
|
2000-08-20 06:49:45 -04:00
|
|
|
|
An additional change to this syntax has also been suggested, to
|
2000-08-23 01:57:47 -04:00
|
|
|
|
generalize the expression given after the `as' clause. Rather
|
|
|
|
|
than a single name, it could be allowed to be any expression that
|
|
|
|
|
yields a valid l-value; anything that can be assigned to. The
|
2000-08-20 06:49:45 -04:00
|
|
|
|
change to accomodate this is minimal, as the patch proves[2], and
|
|
|
|
|
the resulting generalization allows a number of new constructs
|
|
|
|
|
that run completely parallel with other Python assignment
|
2000-08-23 08:09:07 -04:00
|
|
|
|
constructs. However, this idea has been rejected by Guido, as
|
|
|
|
|
`hypergeneralization'.
|
2000-08-15 09:42:44 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Copyright
|
|
|
|
|
|
|
|
|
|
This document has been placed in the Public Domain.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
References
|
|
|
|
|
|
2000-08-23 01:57:47 -04:00
|
|
|
|
[1] http://sourceforge.net/patch/?func=detailpatch&patch_id=101135&group_id=5470
|
2000-08-15 09:42:44 -04:00
|
|
|
|
|
2000-08-23 01:57:47 -04:00
|
|
|
|
[2] http://sourceforge.net/patch/?func=detailpatch&patch_id=101234&group_id=5470
|
2000-08-20 06:49:45 -04:00
|
|
|
|
|
2000-08-15 09:42:44 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Local Variables:
|
|
|
|
|
mode: indented-text
|
|
|
|
|
indent-tabs-mode: nil
|
|
|
|
|
End:
|