@@ -317,7 +317,7 @@ \subsection{The Interactive Startup File \label{startup}}
317317This file is only read in interactive sessions, not when Python reads
318318commands from a script, and not when \file {/dev/tty} is given as the
319319explicit source of commands (which otherwise behaves like an
320- interactive session). It is executed in the same name space where
320+ interactive session). It is executed in the same namespace where
321321interactive commands are executed, so that objects that it defines or
322322imports can be used without qualification in the interactive session.
323323You can also change the prompts \code {sys.ps1} and \code {sys.ps2} in
@@ -3236,24 +3236,24 @@ \section{Python Scopes and Name Spaces \label{scopes}}
32363236
32373237Before introducing classes, I first have to tell you something about
32383238Python's scope rules. Class definitions play some neat tricks with
3239- name spaces , and you need to know how scopes and name spaces work to
3239+ namespaces , and you need to know how scopes and namespaces work to
32403240fully understand what's going on. Incidentally, knowledge about this
32413241subject is useful for any advanced Python programmer.
32423242
32433243Let's begin with some definitions.
32443244
3245- A \emph {name space } is a mapping from names to objects. Most name
3246- spaces are currently implemented as Python dictionaries, but that's
3247- normally not noticeable in any way (except for performance), and it
3248- may change in the future. Examples of name spaces are: the set of
3249- built-in names (functions such as \function {abs()}, and built-in exception
3250- names); the global names in a module; and the local names in a
3251- function invocation. In a sense the set of attributes of an object
3252- also form a name space . The important thing to know about name
3253- spaces is that there is absolutely no relation between names in
3254- different name spaces ; for instance, two different modules may both
3255- define a function `` maximize'' without confusion --- users of the
3256- modules must prefix it with the module name.
3245+ A \emph {namespace } is a mapping from names to objects. Most
3246+ namespaces are currently implemented as Python dictionaries, but
3247+ that's normally not noticeable in any way (except for performance),
3248+ and it may change in the future. Examples of namespaces are: the set
3249+ of built-in names (functions such as \function {abs()}, and built-in
3250+ exception names); the global names in a module; and the local names in
3251+ a function invocation. In a sense the set of attributes of an object
3252+ also form a namespace . The important thing to know about namespaces
3253+ is that there is absolutely no relation between names in different
3254+ namespaces ; for instance, two different modules may both define a
3255+ function `` maximize'' without confusion --- users of the modules must
3256+ prefix it with the module name.
32573257
32583258By the way, I use the word \emph {attribute } for any name following a
32593259dot --- for example, in the expression \code {z.real}, \code {real} is
@@ -3262,13 +3262,13 @@ \section{Python Scopes and Name Spaces \label{scopes}}
32623262\code {modname.funcname}, \code {modname} is a module object and
32633263\code {funcname} is an attribute of it. In this case there happens to
32643264be a straightforward mapping between the module's attributes and the
3265- global names defined in the module: they share the same name
3266- space! \footnote {
3265+ global names defined in the module: they share the same namespace!
3266+ \footnote {
32673267 Except for one thing. Module objects have a secret read-only
3268- attribute called \code {__dict__} which returns the dictionary
3269- used to implement the module's name space ; the name
3270- \code {__dict__} is an attribute but not a global name.
3271- Obviously, using this violates the abstraction of name space
3268+ attribute called \member {__dict__} which returns the dictionary
3269+ used to implement the module's namespace ; the name
3270+ \member {__dict__} is an attribute but not a global name.
3271+ Obviously, using this violates the abstraction of namespace
32723272 implementation, and should be restricted to things like
32733273 post-mortem debuggers.
32743274}
@@ -3280,54 +3280,54 @@ \section{Python Scopes and Name Spaces \label{scopes}}
32803280\samp {del modname.the_answer}.
32813281
32823282Name spaces are created at different moments and have different
3283- lifetimes. The name space containing the built-in names is created
3283+ lifetimes. The namespace containing the built-in names is created
32843284when the Python interpreter starts up, and is never deleted. The
3285- global name space for a module is created when the module definition
3286- is read in; normally, module name spaces also last until the
3285+ global namespace for a module is created when the module definition
3286+ is read in; normally, module namespaces also last until the
32873287interpreter quits. The statements executed by the top-level
32883288invocation of the interpreter, either read from a script file or
32893289interactively, are considered part of a module called
3290- \module {__main__}, so they have their own global name space . (The
3290+ \module {__main__}, so they have their own global namespace . (The
32913291built-in names actually also live in a module; this is called
32923292\module {__builtin__}.)
32933293
3294- The local name space for a function is created when the function is
3294+ The local namespace for a function is created when the function is
32953295called, and deleted when the function returns or raises an exception
32963296that is not handled within the function. (Actually, forgetting would
32973297be a better way to describe what actually happens.) Of course,
3298- recursive invocations each have their own local name space .
3298+ recursive invocations each have their own local namespace .
32993299
3300- A \emph {scope } is a textual region of a Python program where a name space
3301- is directly accessible. `` Directly accessible'' here means that an
3302- unqualified reference to a name attempts to find the name in the name
3303- space .
3300+ A \emph {scope } is a textual region of a Python program where a
3301+ namespace is directly accessible. `` Directly accessible'' here means
3302+ that an unqualified reference to a name attempts to find the name in
3303+ the namespace .
33043304
33053305Although scopes are determined statically, they are used dynamically.
33063306At any time during execution, exactly three nested scopes are in use
3307- (i.e., exactly three name spaces are directly accessible): the
3307+ (i.e., exactly three namespaces are directly accessible): the
33083308innermost scope, which is searched first, contains the local names,
33093309the middle scope, searched next, contains the current module's global
3310- names, and the outermost scope (searched last) is the name space
3310+ names, and the outermost scope (searched last) is the namespace
33113311containing built-in names.
33123312
33133313Usually, the local scope references the local names of the (textually)
33143314current function. Outside of functions, the local scope references
3315- the same name space as the global scope: the module's name space .
3316- Class definitions place yet another name space in the local scope.
3315+ the same namespace as the global scope: the module's namespace .
3316+ Class definitions place yet another namespace in the local scope.
33173317
33183318It is important to realize that scopes are determined textually: the
3319- global scope of a function defined in a module is that module's name
3320- space , no matter from where or by what alias the function is called.
3321- On the other hand, the actual search for names is done dynamically, at
3322- run time --- however, the language definition is evolving towards
3323- static name resolution, at `` compile'' time, so don't rely on dynamic
3324- name resolution! (In fact, local variables are already determined
3325- statically.)
3319+ global scope of a function defined in a module is that module's
3320+ namespace , no matter from where or by what alias the function is
3321+ called. On the other hand, the actual search for names is done
3322+ dynamically, at run time --- however, the language definition is
3323+ evolving towards static name resolution, at `` compile'' time, so don't
3324+ rely on dynamic name resolution! (In fact, local variables are
3325+ already determined statically.)
33263326
33273327A special quirk of Python is that assignments always go into the
33283328innermost scope. Assignments do not copy data --- they just
33293329bind names to objects. The same is true for deletions: the statement
3330- \samp {del x} removes the binding of \code {x} from the name space
3330+ \samp {del x} removes the binding of \code {x} from the namespace
33313331referenced by the local scope. In fact, all operations that introduce
33323332new names use the local scope: in particular, import statements and
33333333function definitions bind the module or function name in the local
@@ -3366,14 +3366,14 @@ \subsection{Class Definition Syntax \label{classDefinition}}
33663366dictated by the calling conventions for methods --- again, this is
33673367explained later.
33683368
3369- When a class definition is entered, a new name space is created, and
3369+ When a class definition is entered, a new namespace is created, and
33703370used as the local scope --- thus, all assignments to local variables
3371- go into this new name space . In particular, function definitions bind
3371+ go into this new namespace . In particular, function definitions bind
33723372the name of the new function here.
33733373
33743374When a class definition is left normally (via the end), a \emph {class
33753375object } is created. This is basically a wrapper around the contents
3376- of the name space created by the class definition; we'll learn more
3376+ of the namespace created by the class definition; we'll learn more
33773377about class objects in the next section. The original local scope
33783378(the one in effect just before the class definitions was entered) is
33793379reinstated, and the class object is bound here to the class name given
@@ -3387,7 +3387,7 @@ \subsection{Class Objects \label{classObjects}}
33873387
33883388\emph {Attribute references } use the standard syntax used for all
33893389attribute references in Python: \code {obj.name}. Valid attribute
3390- names are all the names that were in the class's name space when the
3390+ names are all the names that were in the class's namespace when the
33913391class object was created. So, if the class definition looked like
33923392this:
33933393
@@ -3766,9 +3766,10 @@ \section{Private Variables \label{private}}
37663766when referencing \code {__dict__} directly.
37673767
37683768Here's an example of a class that implements its own
3769- \code {__getattr__} and \code {__setattr__} methods and stores all
3770- attributes in a private variable, in a way that works in Python 1.4 as
3771- well as in previous versions:
3769+ \method {__getattr__()} and \method {__setattr__()} methods and stores
3770+ all attributes in a private variable, in a way that works in all
3771+ versions of Python, including those available before this feature was
3772+ added:
37723773
37733774\begin {verbatim }
37743775class VirtualAttributes:
0 commit comments