In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "how to use closure method". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
What is a Python closure?
First, let me use a simple example to illustrate what a closure in Python is. Look at the following function:
Def outer (): X = 1 def inner (): print (f'x in outer function: {x}') return inner
The property of defining another function inside a function and returning it is a closure. Check the return value of the outer function to confirm that this is a function.
> def outer (): X = 1. Def inner ():... Print (f'x in outer function: {x}')... Return inner... > > outer > outer () >
What can closures do? Because the function returns a function, we can call the function, such as:
Outer () () x in outer function: 1 >
But we usually use closures like this, which is ugly. You may wonder what this has to do with recursion. Don't worry, let's slowly realize the power of closures.
Variable access in closure
Judging from the above run result, the inner function can access the variable x defined within the outer function, but it cannot be modified. The following code will report an error when it runs:
> def outer (): X = 1. Def inner ():... Print (f'x in outer function (before modifying): {x}')... X + = 1. Print (f'x in outer function (after modifying): {x}')... Return inner. > f = outer () > > f () Traceback (most recent call last): File "", line 1, in File "", line 4, in inner UnboundLocalError: local variable 'x' referenced before assignment >
To solve this problem, we can add the nonlocal keyword to tell the inner function that this is not a local variable:
> def outer (): X = 1. Def inner ():... Nonlocal x... Print (f'x in outer function (before modifying): {x}')... X + = 1. Print (f'x in outer function (after modifying): {x}')... Return inner. > f = outer () > f () x in outer function (before modifying): 1 x in outer function (after modifying): 2 > f () x in outer function (before modifying): 2 x in outer function (after modifying): 3 > f () x in outer function (before modifying): 3 x in outer function (after modifying): 4 >
Have you found that the value of x is saved and increases by 1 each time it is called? this is the beauty of closures.
Replace recursion with closures
Taking advantage of the fact that the above closure retains the result of the call, we can use this instead of recursion, such as using the closure to calculate the Fibonacci sequence:
Def fib (): x1 = 0 x2 = 1 def get_next_number (): nonlocal x1, x2 x3 = x1 + x2 x1, x2 = x2, x3 return x3 return get_next_number
It can be used to produce Fibonacci series as follows:
> def fib (): X1 = 0. X2 = 1. Def get_next_number ():... Nonlocal x1, x2... X3 = x1 + x2. X1, x2 = x2, x3. Return x3... Return get_next_number. > > fibonacci = fib () > for i in range (2,21):. Num = fibonacci ()... Print (f'The {I} th Fibonacci number is {num}')... The 2th Fibonacci number is 1 The 3th Fibonacci number is 2 The 4th Fibonacci number is 3 The 5th Fibonacci number is 5 The 6th Fibonacci number is 8 The 7th Fibonacci number is 13 The 8th Fibonacci number is 21 The 9th Fibonacci number is 34 The 10th Fibonacci number is 55 The 11th Fibonacci number is 89 The 12th Fibonacci number is 377 The 14th Fibonacci number is 377 The 15th Fibonacci number is 610 The 16th Fibonacci number is 987 The 17th Fibonacci number is 1597 The 18th Fibonacci number is 2584 The 19th Fibonacci number is 4181 The 20th Fibonacci number is 6765 > >
The recursive method for calculating the Fibonacci sequence is as follows:
Def fib_recursion (n:int)-> int: if n
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.