mirror of https://github.com/python/cpython.git
When referring to namespaces, always say "namespaces" instead of
"name spaces". Inconsistency noted by Keith Briggs <keith.briggs@bt.com>.
This commit is contained in:
parent
81cccb7563
commit
1349437e4c
|
@ -3242,18 +3242,18 @@ subject is useful for any advanced Python programmer.
|
|||
|
||||
Let's begin with some definitions.
|
||||
|
||||
A \emph{name space} is a mapping from names to objects. Most name
|
||||
spaces are currently implemented as Python dictionaries, but that's
|
||||
normally not noticeable in any way (except for performance), and it
|
||||
may change in the future. Examples of name spaces are: the set of
|
||||
built-in names (functions such as \function{abs()}, and built-in exception
|
||||
names); the global names in a module; and the local names in a
|
||||
function invocation. In a sense the set of attributes of an object
|
||||
also form a name space. The important thing to know about name
|
||||
spaces is that there is absolutely no relation between names in
|
||||
different name spaces; for instance, two different modules may both
|
||||
define a function ``maximize'' without confusion --- users of the
|
||||
modules must prefix it with the module name.
|
||||
A \emph{namespace} is a mapping from names to objects. Most
|
||||
namespaces are currently implemented as Python dictionaries, but
|
||||
that's normally not noticeable in any way (except for performance),
|
||||
and it may change in the future. Examples of namespaces are: the set
|
||||
of built-in names (functions such as \function{abs()}, and built-in
|
||||
exception names); the global names in a module; and the local names in
|
||||
a function invocation. In a sense the set of attributes of an object
|
||||
also form a namespace. The important thing to know about namespaces
|
||||
is that there is absolutely no relation between names in different
|
||||
namespaces; for instance, two different modules may both define a
|
||||
function ``maximize'' without confusion --- users of the modules must
|
||||
prefix it with the module name.
|
||||
|
||||
By the way, I use the word \emph{attribute} for any name following a
|
||||
dot --- for example, in the expression \code{z.real}, \code{real} is
|
||||
|
@ -3262,12 +3262,12 @@ names in modules are attribute references: in the expression
|
|||
\code{modname.funcname}, \code{modname} is a module object and
|
||||
\code{funcname} is an attribute of it. In this case there happens to
|
||||
be a straightforward mapping between the module's attributes and the
|
||||
global names defined in the module: they share the same name
|
||||
space!\footnote{
|
||||
global names defined in the module: they share the same namespace!
|
||||
\footnote{
|
||||
Except for one thing. Module objects have a secret read-only
|
||||
attribute called \code{__dict__} which returns the dictionary
|
||||
attribute called \member{__dict__} which returns the dictionary
|
||||
used to implement the module's namespace; the name
|
||||
\code{__dict__} is an attribute but not a global name.
|
||||
\member{__dict__} is an attribute but not a global name.
|
||||
Obviously, using this violates the abstraction of namespace
|
||||
implementation, and should be restricted to things like
|
||||
post-mortem debuggers.
|
||||
|
@ -3297,10 +3297,10 @@ that is not handled within the function. (Actually, forgetting would
|
|||
be a better way to describe what actually happens.) Of course,
|
||||
recursive invocations each have their own local namespace.
|
||||
|
||||
A \emph{scope} is a textual region of a Python program where a name space
|
||||
is directly accessible. ``Directly accessible'' here means that an
|
||||
unqualified reference to a name attempts to find the name in the name
|
||||
space.
|
||||
A \emph{scope} is a textual region of a Python program where a
|
||||
namespace is directly accessible. ``Directly accessible'' here means
|
||||
that an unqualified reference to a name attempts to find the name in
|
||||
the namespace.
|
||||
|
||||
Although scopes are determined statically, they are used dynamically.
|
||||
At any time during execution, exactly three nested scopes are in use
|
||||
|
@ -3316,13 +3316,13 @@ the same name space as the global scope: the module's name space.
|
|||
Class definitions place yet another namespace in the local scope.
|
||||
|
||||
It is important to realize that scopes are determined textually: the
|
||||
global scope of a function defined in a module is that module's name
|
||||
space, no matter from where or by what alias the function is called.
|
||||
On the other hand, the actual search for names is done dynamically, at
|
||||
run time --- however, the language definition is evolving towards
|
||||
static name resolution, at ``compile'' time, so don't rely on dynamic
|
||||
name resolution! (In fact, local variables are already determined
|
||||
statically.)
|
||||
global scope of a function defined in a module is that module's
|
||||
namespace, no matter from where or by what alias the function is
|
||||
called. On the other hand, the actual search for names is done
|
||||
dynamically, at run time --- however, the language definition is
|
||||
evolving towards static name resolution, at ``compile'' time, so don't
|
||||
rely on dynamic name resolution! (In fact, local variables are
|
||||
already determined statically.)
|
||||
|
||||
A special quirk of Python is that assignments always go into the
|
||||
innermost scope. Assignments do not copy data --- they just
|
||||
|
@ -3766,9 +3766,10 @@ code that is byte-compiled together. The same restriction applies to
|
|||
when referencing \code{__dict__} directly.
|
||||
|
||||
Here's an example of a class that implements its own
|
||||
\code{__getattr__} and \code{__setattr__} methods and stores all
|
||||
attributes in a private variable, in a way that works in Python 1.4 as
|
||||
well as in previous versions:
|
||||
\method{__getattr__()} and \method{__setattr__()} methods and stores
|
||||
all attributes in a private variable, in a way that works in all
|
||||
versions of Python, including those available before this feature was
|
||||
added:
|
||||
|
||||
\begin{verbatim}
|
||||
class VirtualAttributes:
|
||||
|
|
Loading…
Reference in New Issue