Detailed description of LEGB, closure, and decorator in Python, and pythonlegb

Source: Internet
Author: User

Detailed description of LEGB, closure, and decorator in Python, and pythonlegb

Detailed explanation of LEGB and closure and decorator in Python

Legb l> E> G? B

  • L: internal scope of the local Function
  • E: Between the enclosing function and the embedded Function
  • G: global scope
  • B: build-in Built-in Scope

Python Closure

1. Closure: Reference to enclosing scope variables in internal functions

2. Functions and attributes

  • A function is an object.
  • Internal Variable recycling after function execution is complete
  • Function attribute
  • Function return value
passline = 60def func(val):  if val >= passline:    print ('pass')  else:    print ('failed')  def in_func():    print (val)  in_func()  return in_funcf = func(89)f()print (f.__closure__)

General

def f_100(val):  passline = 60  if val >= passline:    print ('pass')  else:    print ('failed')def f_150(val):  passline = 90  if val >= passline:    print ('pass')  else:    print ('failed')f_100(89)f_150(89)

Closure

def set_passline(passline):  def cmp(val):    if val >= passline:      print ('Pass')    else:      print ('failed')  return cmpf_100 = set_passline(60)f_150 = set_passline(90)f_100(89)f_150(89)

Closure: Reference to the enclosing scope variable in the internal function. It will pass the enclosing scope variable to the closure of the internal function.

Function of closure:

  • Encapsulation
  • Code reuse

Python closure 2

Sum

Def my_sum (* arg): if len (arg) = 0: return 0 for val in arg: if not isinstance (val, int ): # if there is a non-int value, 0 return 0 return sum (arg) def my_average (* arg): if len (arg) = 0: return 0 for val in arg: if not isinstance (val, int): # if there is a non-int, 0 return 0 return sum (arg)/len (arg) print (my_sum (, 5) is returned )) print (my_sum (, 5, '6') print (my_aveage (, 5) print (my_average ())

Use of closures

Def my_sum (* arg): return sum (arg) def my_average (* arg): return sum (arg)/len (arg) def dec (func ): def in_dec (* arg): # my_sum print ('in dec arg = ', arg) if len (arg) = 0: return 0 for val in arg: if not isinstance (val, int): return 0 return func (* arg) # closure, which exists in _ closure _ of the in_dec function, therefore, you can call return in_decmy_sum = dec (my_sum) # print (my_sum (, 5) (my_sum (, 5, '6 ')) # my_sum is the in_dec function. The parameter type judgment is executed first, and then the my_sum function in _ closure _ is executed.

Python decorator

  1. The decorator is used to decorate functions.
  2. Returns a function object.
  3. The decorated Function Identifier points to the returned function object.
  4. Syntactic sugar @ deco

How to Use the decorator

Def dec (func): def in_dec (* arg): # my_sum print ('in dec arg = ', arg) if len (arg) = 0: return 0 for val in arg: if not isinstance (val, int): return 0 return func (* arg) # closure, which exists in _ closure _ of the in_dec function, so you can call return in_dec # If no return value is returned, my_sum will call the decorator and then be None # my_sum = dec (my_sum) # Do not manually pass the parameter @ dec # The modifier passes my_sum as the parameter to dec, and returns a new function assigned to my_sumdef my_sum (* arg): return sum (arg) def my_average (* arg): return sum (arg)/len (arg) print (my_sum (1, 2, 3, 4, 5) print (my_sum (1, 2, 3, 4, 5, '6 '))

Another example

def deco(func):  def in_deco(x,y):    print ('in deco')    func(x,y)  print ('call deco')  return in_deco@decodef bar(x, y):  print ('in bar',x+y)bar(1,2)

The above is the introduction of LEGB, closure, and decorator in Python. If you have any questions, please leave a message or go to the community on this site for discussion. There are still many articles about Python on this site. I hope you can search for them, thank you for reading this article. I hope it will help you. Thank you for your support for this site!

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.