function scope of Python adorner

Source: Internet
Author: User
Tags closure

1. Function Scope LEGB

l:local function Internal Scope

e:enclosing function inside and inline function

G:global Global scope

B:build-in built-in scopes

Passline = 60deffunc (val): Passline= 90ifVal >=Passline:Print('Pass')    Else:        Print('failed')    defIn_func ()Print(val) in_func ()defMax (val1,val2):returnMax (Val1,val2) func (89)Print(Max (90,100))

2. Closure understanding and use

Closure concept: Closure: An intrinsic function that references a variable in a enclosing scope

function Essence and property

1: function is an object

2: Internal variable recycling after function execution completes

3: Function Properties

4: function return value

Closure function

1: Package

2: Code Reuse

Passline = 60# -deffunc (val):Print('%x'%ID (val)) Passline= 90ifVal >=Passline:Print('Pass')    Else:        Print('failed')    defIn_func ():#(Val,)        Print(val) in_func ()returnIN_FUNCF= Func (89) F ()Print(F.__closure__)


29198a8failed8989(<cell at 0x7fe62bbf42f0:int Object @ 0x29198a8> in 0.0S]
deffunc_150 (val): Passline= 90ifVal >=Passline:Print('%d pass'%val)Else:        Print('failed')    deffunc_100 (val): Passline= 60ifVal >=Passline:Print('%d pass'%val)Else:        Print('failed')defSet_passline (passline):defCMP (val):ifVal >=Passline:Print('Pass')        Else:            Print('failed')    returncmpf_100= Set_passline (60) f_150= Set_passline (90)Print(Type (f_100))Print(f_100.__closure__) f_100 (89) f_100 (59) f_150 (89) f_150 (59)
Pass in 0.0s]
defMy_averge (*Arg):Print('In My_averge')    returnSUM (ARG)/Len (ARG)defMy_sum (*Arg):Print('In my_sum')    returnsum (ARG)defDec (func):defIn_dec (*Arg):Print('In dec arg =', Arg)ifLen (arg) = =0:return0 forValinchARG:if  notisinstance (val, int):return0returnFunc (*Arg)returnIn_dec#Dec return in_dec-My_sum#my_sum = In_dec (*arg)My_sum =Dec (my_sum)Print(My_sum (1, 2, 3, 4, 5))Print(My_sum (1, 2, 3, 4, 5,'6'))Print(My_averge (1, 2, 3, 4, 5)) My_averge=Dec (my_averge)Print(My_averge (1, 2, 3, 4, 5,'6'))
('In dec arg =', (1, 2, 3, 4, 5))inchMy_sum15('In dec arg =', (1, 2, 3, 4, 5,'6')) 0inchMy_averge3('In dec arg =', (1, 2, 3, 4, 5,'6')) 0[finishedinch0.0S]

3. Decorative Device

1: Decorator for decorating functions

2: Returns a Function object

3: The decorated function identifier points to the returned function object

4: Grammar Sugar @deco

defDec (func):Print('Call Dec')    defIn_dec (*arg):#My_sum        Print('In dec arg =', Arg)ifLen (arg) = =0:return0 forValinchARG:if  notisinstance (val, int):return0returnFunc (*Arg)returnIn_dec@decdefMy_sum (*arg):#my_sum = In_dec    Print('In my_sum')    returnsum (ARG)Print(My_sum (1, 2, 3, 4, 5))#my_sum = Dec (my_sum) #Deco (BAR)-In_deco#bar = In_deco#Bar () In_deco () bar ()defDeco (func):defIn_deco (x, y):Print('In deco') func (x, y)Print('Call Deco')    returnIn_deco@decodefBar (x, y):Print('In bar', x+y)Print(Type (bar)) Bar ()
Call Dec (", (1, 2, 3, 4, 5)) inch my_sumcalldeco'function'> in  deco (  'inbar', 3 in 0.0s]

Python adorner function scope

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.