Three Ways to bind names to values

This is the way to abstract.

import: Built-in functions.

Assignment:

 We can assign a function to a variable, just like this:
f = max
f(1 , 2)   # 2
max(1 , 2) # 2

Execution rule for assignment statements: 1. Evaluate all expressions to the right of = from left to right. 2. Bind all names to the left of = to the resulting values in the current frame. Every expression is evaluated in the context of an environment.
And the changes is in the current frame. When you find the name in parent/global frame, the changes WILL NOT affect the variable.

Keywords in defining a variable
  • nonlocal:
    It is used to work with variables inside nested functions, where the variable should not belong to the outer function instead of inner function and can be changed by inner function.
    Use the keyword nonlocal to declare that the variable is the one in the outer function. Then it will be monopolized by the function created by the outer function. Every time it be called, the nonlocal variable will remind the value of this variable.

In the other hand, variable in other functions could not be accessed in other function unless it is nested. Because they are in different Environment.

There is an example:

def make_test_dice(*outcomes):
    assert len(outcomes) > 0, 'You must supply outcomes to make_test_dice'
    for o in outcomes:
        assert type(o) == int and o >= 1, 'Outcome is not a positive integer'
    index = len(outcomes) - 1
    def dice():
        nonlocal index
        index = (index + 1) % len(outcomes)
        return outcomes[index]
    return dice

Every time we call make_test_dice to create a new function, it will have its index with it.
Every time we call the same dice created by make_test_dice, it will record the change of index.
So this is called Higher-Order Function.

Create own Higher-Order Function.

  • def: Define.
def <name> (<formal parameters>): # signiture (How many argument it takes)
	return <ret expression> # body (what to do)
  • Execution procedure for def statements:

    1. Create a function with signature <name>(<formal parameters>).It let us knows how create a struct local frame when we evaluate it(name and parameter(s)).Call multiple, create multiple frame.
    2. Set the body of that function to be everything indented after the first line
    3. Bind <name> to that function in the current frame
  • lambda expression: Define some easy function.

  • Similarity and difference between def and lambda:

    • Both create a function with the same domain, range, and behavior.
    • Both functions have as their parent the frame in which they were defined.
    • Both bind that function to the name square.
    • Only the def statement gives the function an intrinsic name, whereas the lambda do not until assigning it to a name.