@@ -431,9 +431,9 @@ \section{Objects, values and types}
431431
432432Every object has an identity, a type and a value. An object's {\em
433433identity} never changes once it has been created; think of it as the
434- object's (permanent) address. An object's {\em type} determines the
435- operations that an object supports (e.g., does it have a length?) and
436- also defines the `` meaning'' of the object's value. The type also
434+ object's address in memory . An object's {\em type} determines the
435+ operations that an object supports (e.g., `` does it have a length?'' )
436+ and also defines the `` meaning'' of the object's value. The type also
437437never changes. The {\em value} of some objects can change; whether
438438this is possible is a property of its type.
439439
@@ -450,36 +450,149 @@ \section{Objects, values and types}
450450Note that the use of the implementation's tracing or debugging
451451facilities may keep objects alive that would normally be collectable.
452452
453- (Some objects contain references to `` external'' resources such as
454- open files. It is understood that these resources are freed when the
455- object is garbage-collected, but since garbage collection is not
456- guaranteed, such objects also provide an explicit way to release the
457- external resource (e.g., a \verb \close \ method). Programs are strongly
458- recommended to use this.)
453+ Some objects contain references to `` external'' resources such as open
454+ files or windows. It is understood that these resources are freed
455+ when the object is garbage-collected, but since garbage collection is
456+ not guaranteed to happen, such objects also provide an explicit way to
457+ release the external resource, usually a \verb \close \ method.
458+ Programs are strongly recommended to always explicitly close such
459+ objects.
459460
460461Some objects contain references to other objects. These references
461462are part of the object's value; in most cases, when such a
462463`` container'' object is compared to another (of the same type), the
463464comparison applies to the {\em values} of the referenced objects (not
464465their identities).
465466
466- Types affect almost all aspects of objects.
467- Even object identity is affected in some sense: for immutable
468- types, operations that compute new values may actually return a
469- reference to any existing object with the same type and value, while
470- for mutable objects this is not allowed. E.g., after
467+ Types affect almost all aspects of an object's life. Even the meaning
468+ of object identity is affected in some sense: for immutable types,
469+ operations that compute new values may actually return a reference to
470+ any existing object with the same type and value, while for mutable
471+ objects this is not allowed. E.g., after
471472
472473\begin {verbatim }
473474a = 1; b = 1; c = []; d = []
474475\end {verbatim }
475476
476- \verb \a \ and \verb \b \ may or may not refer to the same object, but
477- \verb \c \ and \verb \d \ are guaranteed to refer to two different, unique,
478- newly created lists.
477+ \verb \a \ and \verb \b \ may or may not refer to the same object with the
478+ value one, depending on the implementation, but \verb \c \ and \verb \d \
479+ are guaranteed to refer to two different, unique, newly created empty
480+ lists.
481+
482+ \section {Code blocks, execution frames, and name spaces }
483+
484+ A `` code block'' is a piece of Python program text that can be
485+ executed as a unit, such as a module, a class definition or a function
486+ body. Some code blocks (like modules) are executed only once, others
487+ (like function bodies) may be executed many times. Code block may
488+ textually contain other code blocks. Code blocks may invoke other
489+ code blocks (that aren't textually contained) as part of their
490+ execution.
491+
492+ Each command typed interactively is a separate code block; a script
493+ file is a code block; the string argument passed to the built-in
494+ functions \verb \eval \ and \verb \exec \ are code blocks; the expression
495+ read and evaluated by the built-in function \verb \input \ is a code
496+ block.
497+
498+ A code block is executed in an `` execution frame'' . An execution
499+ frame contains some administrative information (used for debugging),
500+ determines where and how execution continues after the code block's
501+ execution has completed, and (perhaps most importantly) defines two
502+ `` name spaces'' that affect execution of the code block.
503+
504+ A name space is a mapping from names (identifiers) to objects. A
505+ particular name space may be referenced by more than one execution
506+ frame, and from other places as well. Adding a name to a name space
507+ is called `` binding'' a name (to an object); changing the mapping of a
508+ name is called `` rebinding'' ; removing a name from the name space is
509+ called `` unbinding'' . Name spaces are functionally equivalent to
510+ dictionaries (described below).
511+
512+ The `` local name space'' of an execution frame determines the default
513+ place where names are defined and searched. The `` global name
514+ space'' determines the place where names listed in \verb \global \
515+ statements are defined and searched, and where names that are not
516+ explicitly bound in the current code block are searched.
517+
518+ Whether a name is local or global in a code block is determined by
519+ static inspection of the source text for the code block: in the
520+ absence of \verb \global \ statements, a name that is bound anywhere in
521+ the code block is local in the entire code block; all other names are
522+ considered global. The \verb \global \ statement forces global
523+ interpretation of selected names throughout the code block. The
524+ following constructs bind names: formal parameters, \verb \import \
525+ statements, class and function definitions (these bind the class or
526+ function name), and targets that are identifiers if occurring in an
527+ assignment, \verb \for \ loop header, or \verb \except \ clause header.
528+ (A target occurring in a \verb \del \ statement does not bind a name.)
529+
530+ When a global name is not found in the global name space, it is
531+ searched in the list of `` built-in'' names (this is actually the
532+ global name space of the module \verb \builtin \). When a name is not
533+ found at all, the \verb \NameError \ exception is raised.
534+
535+ The following table lists the meaning of the local and global name
536+ space for various types of code blocks. The name space for a
537+ particular module is automatically created when the module is first
538+ referenced.
479539
480- \section {Execution frames, name spaces, and scopes }
540+ \begin {center }
541+ \begin {tabular }{|l|l|l|l|}
542+ \hline
543+ Code block type & Global name space & Local name space & Notes \\
544+ \hline
545+ Module & n.s. for this module & same as global & \\
546+ Script & n.s. for \verb \__main__ \ & same as global & \\
547+ Interactive command & n.s. for \verb \__main__ \ & same as global & \\
548+ Class definition & global n.s. of containing block & new n.s. & \\
549+ Function body & global n.s. of containing block & new n.s. & \\
550+ String passed to \verb \exec \ or \verb \eval \
551+ & global n.s. of caller & local n.s. of caller & (1) \\
552+ File read by \verb \execfile \
553+ & global n.s. of caller & local n.s. of caller & (1) \\
554+ Expression read by \verb \input \
555+ & global n.s. of caller & local n.s. of caller & \\
556+ \hline
557+ \end {tabular }
558+ \end {center }
559+
560+ Notes:
561+ \begin {description }
562+ \item [(1)] The global and local name space for these functions can be
563+ overridden with optional extra arguments.
564+ \end {description }
565+
566+ \section {Exceptions }
567+
568+ Exceptions are a means of breaking out of the normal flow of control
569+ of a code block in order to handle errors (or other exceptional
570+ conditions). An exception is `` raised'' at the point where the error
571+ is detected; it may be `` handled'' by the surrounding code block or by any
572+ code block that directly or indirectly invoked the code block where
573+ the error occurred.
574+
575+ The Python interpreter raises an exception when it detects an run-time
576+ error (such as division by zero). A Python program can also
577+ explicitly raise an exception with the \verb \raise \ statement.
578+ Exception handlers are specified with the \verb \try...except \ statement.
481579
482- XXX code blocks, scopes, name spaces, name binding, exceptions
580+ Python uses the `` termination'' model of error handling: a handler can
581+ find out what happened and continue execution at an outer level, but
582+ it cannot repair the cause of the error and retry the failing
583+ operation (except by re-entering the the offending piece of code from
584+ the top).
585+
586+ When an exception is not handled at all, the interpreter terminates
587+ execution of the program, or returns to its interactive main loop.
588+
589+ Exceptions are identified by string objects. Two different string
590+ objects with the same value identify different exceptions.
591+
592+ When an exception is raised, an object (maybe \verb \None \) is passed
593+ as the exception's `` parameter'' ; this object does not affect the
594+ selection of an exception handler, but is passed to the selected
595+ exception handler as additional information.
483596
484597\chapter {The standard type hierarchy }
485598
@@ -700,7 +813,7 @@ \chapter{The standard type hierarchy}
700813Modules are imported by the \verb \import \ statement (see section
701814\ref {import }). A module object is a container for a module's name
702815space, which is a dictionary (the same dictionary as referenced by the
703- \ver \func _globals\ attribute of functions defined in the module).
816+ \verb \func_globals \ attribute of functions defined in the module).
704817Module attribute references are translated to lookups in this
705818dictionary. A module object does not contain the code object used to
706819initialize the module (since it isn't needed once the initialization
@@ -1069,9 +1182,32 @@ \subsection{Calls}
10691182The primary must evaluate to a callable object (user-defined
10701183functions, built-in functions, methods of built-in objects, class
10711184objects, and methods of class instances are callable). If it is a
1072- class, the argument list must be empty.
1185+ class, the argument list must be empty; otherwise, the arguments are
1186+ evaluated.
1187+
1188+ A call always returns some value, possibly \verb \None \, unless it
1189+ raises an exception. How this value is computed depends on the type
1190+ of the callable object. If it is:
1191+
1192+ \begin {description }
10731193
1074- XXX explain what happens on function call
1194+ \item [a user-defined function:] the code block for the function is
1195+ executed, passing it the argument list. The first thing the code
1196+ block will do is bind the formal parameters to the arguments. When
1197+ the code block executes a \verb \return \ statement, this specifies the
1198+ return value of the function call.
1199+
1200+ \item [a built-in function or method:] the result is up to the
1201+ interpreter; see the library reference manual for the descriptions of
1202+ built-in functions and methods.
1203+
1204+ \item [a class object:] a new instance of that class is returned.
1205+
1206+ \item [a class instance method:] the corresponding user-defined
1207+ function is called, with an argument list that is one longer than the
1208+ argument list of the call: the instance becomes the first argument.
1209+
1210+ \end {description }
10751211
10761212\section {Factors }
10771213
@@ -1501,9 +1637,9 @@ \section{The {\tt pass} statement}
15011637required syntactically, but no code needs to be executed, for example:
15021638
15031639\begin {verbatim }
1504- def f(arg): pass # a no-op function
1640+ def f(arg): pass # a function that does nothing (yet)
15051641
1506- class C: pass # an empty class
1642+ class C: pass # an class with no methods (yet)
15071643\end {verbatim }
15081644
15091645\section {The {\tt del} statement }
@@ -1844,7 +1980,7 @@ \section{The {\tt try} statement}
18441980
18451981\begin {verbatim }
18461982try_stmt: "try" ":" suite
1847- ("except" condition ["," condition ] ":" suite)*
1983+ ("except" condition ["," target ] ":" suite)*
18481984 ["except" ":" suite]
18491985 ["finally" ":" suite]
18501986\end {verbatim }
@@ -1859,19 +1995,44 @@ \section{The {\tt try} statement}
18591995The \verb \try...except \ form specifies one or more exception handlers.
18601996When no exception occurs in the \verb \try \ clause, no exception
18611997handler is executed. When an exception occurs in the \verb \try \
1862- suite, a search for an exception handler HIRO
1998+ suite, a search for an exception handler is started. This inspects
1999+ the except clauses (exception handlers) in turn until one is found
2000+ that matches the exception. A condition-less except clause (which
2001+ must be last) matches any exception. For except clause with a
2002+ condition, that condition is evaluated, and the clause matches the
2003+ exception if the resulting object is `` compatible'' with the
2004+ exception. An object is compatible with an exception if it is either
2005+ the object that identifies the exception or it is a tuple containing
2006+ an item that is compatible with the exception.
2007+
2008+ If no except clause matches the exception, the search for an exception
2009+ handler continues in the surrounding code and on the invocation stack.
2010+
2011+ If the evaluation of a condition in the header of an except clause
2012+ raises an exception, the original search for a handler is cancelled
2013+ and a search starts for the new exception in the surrounding code and
2014+ on the call stack.
2015+
2016+ When a matching except clause is found in a try statement, the
2017+ exception's parameter is assigned to the target specified in the
2018+ except clause (if present), and the except clause's suite is executed.
2019+ When the end of this suite is reached, execution continues normally
2020+ at the point following the entire try statement. (This means that if
2021+ two nested handlers exist for the same exception, and the exception
2022+ occurs in the try clause of the inner handler, the outer handler will
2023+ not notice the exception.)
18632024
18642025The \verb \try...finally \ form specifies a `cleanup' handler. The
18652026\verb \try \ clause is executed. When no exception occurs, the
1866- \verb \finally \ clause is executed. When an exception occurs on the
2027+ \verb \finally \ clause is executed. When an exception occurs in the
18672028\verb \try \ clause, the exception is temporarily saved, the
18682029\verb \finally \ clause is executed, and then the saved exception is
18692030re-raised. If the \verb \finally \ clause raises another exception or
18702031executes a \verb \return \, \verb \break \ or \verb \continue \ statement,
18712032the saved exception is lost.
18722033
18732034When a \verb \return \ or \verb \break \ statement is executed in the
1874- \verb \try suite of a \verb\try ...finally\ statement, the
2035+ \verb \try \ suite of a \verb \try...finally \ statement, the
18752036\verb \finally \ clause is also executed `on the way out'. A
18762037\verb \continue \ statement is illegal in the \verb \try \ clause (the
18772038reason is a problem with the current implementation -- this
@@ -1895,15 +2056,15 @@ \section{Class definitions} \label{class}
18952056
18962057\begin {verbatim }
18972058classdef: "class" identifier [inheritance] ":" suite
1898- inheritance: "(" expression ("," expression)* ")"
2059+ inheritance: "(" condition_list ")"
18992060\end {verbatim }
19002061
19012062XXX
19022063
19032064\section {P.M. }
19042065
19052066XXX Syntax for scripts, modules
1906- XXX Syntax for interactive input, eval, exec, input
2067+ XXX Syntax for interactive input, eval, exec, execfile, input
19072068XXX New definition of expressions (as conditions)
19082069
19092070\end {document }
0 commit comments