SoFunction
Updated on 2024-12-14

LEGB and closures and decorators in Python in detail

LEGB and closures and decorators in Python in detail

LEGB L>E>G?B

  • L:local function inner scope
  • E:enclosing functions within and between inline functions
  • G:global global scope
  • B:build-in built-in scope

python closures

:References to enclosing scope variables in internal functions

2. Function substance and properties

  • A function is an object
  • Internal variable recovery after function execution is complete
  • function property
  • Function Return Value
passline = 60
def func(val):
  if val >= passline:
    print ('pass')
  else:
    print ('failed')
  def in_func():
    print (val)
  in_func()
  return in_func

f = 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 (math)

def set_passline(passline):
  def cmp(val):
    if val >= passline:
      print ('Pass')
    else:
      print ('failed')
  return cmp

f_100 = set_passline(60)
f_150 = set_passline(90)
f_100(89)
f_150(89)

Closure: a reference to the enclosing scope variable in the inner function, which passes the enclosing scope variable to the closure of the inner function.

The role of closures:

  • seal inside
  • code reuse

python closures II

look for a draw (chess)

def my_sum(*arg):
  if len(arg) == 0:
    return 0
  for val in arg:
    if not isinstance(val,int): # Return 0 if there is a non-int
      return 0
  return sum(arg)

def my_average(*arg):
  if len(arg) == 0:
    return 0
  for val in arg:
    if not isinstance(val,int): # Return 0 if there is a non-int
      return 0
  return sum(arg)/len(arg)

print (my_sum(1,2,3,4,5))
print (my_sum(1,2,3,4,5,'6'))
print (my_aveage(1,2,3,4,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 the __closure__ in the in_dec function, so you can call the
  return in_dec

my_sum = dec(my_sum)  # Pass the parameter

print(my_sum (1,2,3,4,5))
print(my_sum (1,2,3,4,5,'6'))
# my_sum is the in_dec function that performs a parameter type determination, then executes the function my_sum in __closure__

python decorator

  1. Decorators are used to decorate functions
  2. Returns a function object
  3. The decorated function identifier points to the returned function object.
  4. Syntactic Sugar @deco

Ways to use decorators

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 the __closure__ in the in_dec function, so you can call the
  return in_dec  # None after my_sum calls the decorator if there is no return value

# my_sum = dec(my_sum) # no manual passing of parameters
@dec     # The decorator passes my_sum as an argument to dec and returns a new function assignment to my_sum
def 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

@deco
def bar(x, y):
  print ('in bar',x+y)

bar(1,2)

The above is the introduction of LEGB and closures and decorators in Python, if you have any questions, please leave a message or go to this site to exchange discussions in the community, this site has a lot of articles about Python, but also hope that you search for access, thanks for reading, I hope to help you, thank you for your support of this site!