In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "example Analysis of Python interview questions". In daily operation, I believe that many people have doubts about the analysis of Python interview questions. The editor has consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts of "Python interview questions example Analysis". Next, please follow the editor to study!
Inadvertently, I saw such a Python interview question: what will the following code output?
Def testFun (): temp = [lambda x: iTunx for i in range (4)] return temp for everyLambda in testFun (): print (everyLambda (2))
Thinking silently in my head, needless to say, it must be:
0 2 4 6
* * at first glance, the answer is:
6 6 6
So with a skeptical state of mind (in fact, do not admit defeat, do not admit mistakes), open the editor, a quick knock, sure enough:
Suspected the life for a long time, originally wanted to be dark, WTF Python … Then I realized that I was too rusty.
* it is found that the reason is the late binding caused by the late binding of the Python closure, which means that the variables in the closure are looked up when the internal function is called. So the result is that when any function returned by testFun () is called, at that time, the value of I is looked up in the scope around it when it is called, and by then, no matter which returned function is called, the for loop is complete, and the value of I * * is 3, so the value of each returned function testFun is 3. So a value equal to 2 is passed into the above code, and they return a value of 6 (for example: 3 x 2).
How on earth can such a result be achieved?
0 2 4 6
After thinking about it, if you can bind parameters immediately, or simply do not need to close the total line, use another way to avoid I rewriting.
Recalling what I have learned before, four solutions have been conceived:
* species: create a closure and bind its parameters immediately by using default parameters
Def testFun (): temp = [lambda x, iTuni: iTunx for i in range (4)] return temp for everyLambda in testFun (): print (everyLambda (2))
Second: use the functools.partial function to fix some parameters of the function (with or without default values) (that is, equivalent to setting default values)
From functools import partial from operator import mul def testFun (): return [partial (mul,i) for i in range (4)] for everyLambda in testFun (): print (everyLambda (2))
The third: elegant writing, directly using the generator
Def testFun (): return (lambda x, iTuni: iTunx for i in range (4)) for everyLambda in testFun (): print (everyLambda (2))
The fourth: the idea of using yield's laziness to evaluate.
Def testFun (): for i in range (4): yield lambda x: iTunx for everyLambda in testFun (): print (everyLambda (2))
Final running result:
At this point, the study of "example Analysis of Python interview questions" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.