4 Python base closure with decorator

 

== Closure closure:
  the closure means to call this function outer nested function scope variable function;

  Closures must meet three conditions noted that the closure does not release :( variables have memory consumption)
    1. There must be built-in functions;
    2. The embedded function must reference an external function variables;
    3. external function return value must be within embedded function ;

Examples closure.py
 # Question: 
# how to write a function, so this function by calling parameter y can generate power of a function y of x? 
# As follows: 
DEF make_power (y):
     DEF the Fn (x):     # Note here, the inline function 
        return X ** Y     # inline function reference variables outer nested function Y 
    return fn     # external function returns nested function 

pow2 = make_power (2 ) where # is returned fn function object
 print ( ' square is 5: ' , pow2 (5))     # 25   
Pow3 make_power = (. 3 )
 Print ( ' . 3 power is 6: ' , Pow3 (6))    #216

 

# Quadratic equation, known x, find the value of y? 
# A * B * X ** 2 + X + Y C = 

DEF get_fx (A, B, C):
     DEF FX (X):
         return A * + B * X ** X ** 2 +. 1 C
     return FX 

f123 = get_fx (. 1, 2,. 3 )
 Print ( ' X = 20 is, Y = ' , f123 (20 is ))
 Print ( ' X = 50, Y = ' , f123 (50 )) 

f654 = get_fx (. 6,. 5,. 4 )
 Print ( ' X = 20 is, Y = ' , f654 (20 is))

Guess you like

Origin www.cnblogs.com/pineliao/p/12097175.html