ZhangZhihui's Blog  

A closure is a nested function that can access free variables from an enclosing function even after it has finished its execution. We know that, like nested function definitions, lambda expressions can reference values from the enclosing scope, so lambda expressions are also useful as a closure.

复制代码
def func(x, username):
     if x < 0:
        return lambda: print('Hello', username)
     elif x > 0:
        return lambda: print('Hi', username)
     else:
        return lambda: print('Hey', username)


f1 = func(6, 'Sam')
f1()
f2 = func(0, 'Tim')
f2()
复制代码

Output-

Hi Sam

Hey Tim

We can use the parameter username inside the lambda expression, so here these lambda expressions act as closures. Here is one more example:

复制代码
def func(symbol):
   return lambda message: print(message + symbol)


exclaim = func('!!!!!')
question = func('???')
sentence = func('.')
exclaim('OMG')
question('What is this')
sentence('Python is easy')
exclaim('Really')
复制代码

Output-

OMG!!!!!

What is this???

Python is easy.

Really!!!!!

The lambda expression remembers the value of the symbol from the enclosing scope even after the flow of control is not in that scope; thus, it acts as a closure.

The function func returns a function object. That function object is created by the lambda expression. message is the parameter of the lambda expression and symbol is the parameter of the function func. We have called the function func 3 times, first time with argument '!!!!!', second time with argument '???' and third time with argument '.'. These arguments will be assigned to the parameter symbol when func is called.

When the function func is executed, the lambda expression creates a function object which is returned by func. We have assigned the returned function objects to the names exclaim, question, and sentence. These names now act like functions and we call them with different arguments. The arguments that are sent to these function calls, are assigned to the parameter message of lambda expression.

 

posted on   ZhangZhihuiAAA  阅读(7)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2023-07-31 Go - go.work, go.mod, go.sum
2023-07-31 Go - installation
2023-07-31 Go - env
2023-07-31 Python - match case
 
点击右上角即可分享
微信分享提示