Title: Theory / Important / CS / Programming / Python / Manual{ / 4 Execution model
Tags: hasNotNewEffectReturnsMeaning FromInstruction computer_to_instruction some_instruction FromVariables fromMath

Name: A script file
Definition: A file given as standard input to the interpreter or specified as a command line argument to the interpreter
Chapter: 4
Section: 1

Name: A script command
Definition: a command specified on the interpreter command line with the ‘-c’ option
Chapter: 4
Section: 1

Name: An execution frame
Definition: Where a code block is executed. It contains some administrative information (used for debugging) and determines where and how execution continues after the code block’s execution has completed
Chapter: 4
Section: 1

Name: A name
Definition: Something which refers to objects
Chapter: 4
Section: 2.1

Name: A binding operation
Definition: Stating that a name refer to some object
Chapter: 4
Section: 2.1

Name: A free variable
Definition: A variable used in a code block but not defined there
Chapter: 4
Section: 2.1

Name: A local variable of b
Definition: A variable bound in b not declared nonlocal nor global
Chapter: 4
Section: 2.1
Variables: b a block

Name: A global variable
Definition: A variable bound in the module level
Chapter: 4
Section: 2.1
Variables: b a block

Name: The scope of n
Definition: The visibility of n within a block
Chapter: 4
Section: 2.2
Variables: n a name

Question: How to resolve n?
Answer: using the nearest enclosing scope.
Chapter: 4
Section: 2.2
Variables: n a name
Extra: Note that, if this name is binded in this scope after n is encountered, it will leads to an error, even if n is also binded before in a less near enclosing scope

Name: The block's environment
Definition: The set of all scopes visibles to b
Chapter: 4
Section: 2.2
Variables: b a block.
Context: Python

Instruction: global identifier
Effect: All uses of identifier specified in the statement refer to the binding of that name in the top-level namespace, and in the builtins namespace
Language: Python
Chapter: 4
Section: 2.2, 12

Name: builtin namespace
Definition: Namespace of the module builtin
Chapter: 4,
Section: 2.2,

Instruction: nonlocal identifier
Effect: identifier refers to previously bound variables in the nearest enclosing function scope.
Language: Python
Chapter: 4
Section: 2.2,

Variable: __main__
Meaning: The main module for a script
Language: Python
Section: 2.2,
Chapter: 4

Question: The scope of names defined in a class block is
Answer: limited to the class block; it does not extend to the code blocks of methods – this includes comprehensions and generator expressions since they are implemented using a function scope
Chapter: 4,
Section: 2.2,

Question: Name resolution of free variables occurs at
Answer: runtime
Chapter: 4
Section: 2.4

Question: Name resolution of an eval/exec
Answer: is either local or global namespace, but not enclosing namespace
Chapter: 4
Section: 2.4

Question: When an exception is raised ?
Answer: The execution goes back to the last try ... except, which deals with the exception.
If no such try except exists:
-the interactive main loopp print stack backtrace, except when the exception is SystemExit
-otherwise the interpreter halts
Chapter: 4
Section: 2.4

Instruction: NameError
Effect: There is a reference to an unbound name
Language: Python
Chapter: 4
Section: 2.2

Instruction: UnboundLocalError
Effect: Reference to a local variable not yet bound
Language: Python
Chapter: 4
Section: 2.2
Typ: subclass of NameError

Name: Binding names
Definition: formal parameters to functions
Chapter: 4
Section: 2.1
Definition2: import statements. This form may only be used at the module level.
Definition3: class and function definitions (these bind the class or function name in the defining block), and targets that are identifiers if occurring in an assignment
Definition4: for loop header
Definition5: after as in a withstatement or except clause.

Name: A block
Definition: String arguments passed to eval or exec
Chapter: 4
Section: 1
Context: Python
Example: module, function body, class definition
Example2: command typed interactively
Example3: a script command
Example4: a module run as a top level script (as module __main__)

Name: Handling the exception E
Definition: bounding the exception E in a try ... except
Page: https://docs.python.org/3/reference/executionmodel.html#exceptions
Chapter: 4
Section: 3
Variables: E an exception

Name: The termination model
Definition: When handling an exception, you can not repair and restart. You can look at the stack of the exception but can't use it to run from it
Page: https://docs.python.org/3/reference/executionmodel.html#exceptions
Chapter: 4
Section: 3
Context: error handling

Question: What occurs if an exception is not handled
Answer: the interpreter terminates execution of the program, or returns to its interactive main loop
Page: https://docs.python.org/3/reference/executionmodel.html#exceptions
Chapter: 4
Section: 3
Context: Python
Answer2: it prints a stack traceback, except when the exception is SystemExit.

Instruction: (expr)
Page: https://docs.python.org/3/reference/expressions.html#parenthesized-forms
Chapter: 6
Section: 2.3
Returns: expr
Name: parenth_form
Intuition: Allow to control priority of operation

Question: In a comprehension
for x_1 in l_1 for x_2 in l_2
in which order are the loops interpreted
Answer: from left to right. Similar to
for x_1 in l_1:
  for x_2 in l_2:
Page: https://docs.python.org/3/reference/expressions.html#parenthesized-forms
Chapter: 6
Section: 2.3

Instruction: *it
Page: https://docs.python.org/3/reference/expressions.html#parenthesized-forms
Effect: same as listing all elements of it in the comprehension explicitly
Language: python
Chapter: 6
Section: 15
Variables: it an iterable
Where to use it: In a comprehension
Name: iterable unpacking