In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the knowledge of "how to use Python function objects and closures". In the operation of actual cases, many people will encounter such a dilemma, so 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!
A function object
Function object means that a function can be treated as' data', which can be divided into four aspects.
1.1Functions can be referenced > def add (XMagol y):... Return Xavier... > func=add > func (1Magne2) 31.2 function can be used as an element of container type > dic= {'add':add,'max':max} > dic {' add':, 'max':} > dic [' add'] (1pime2) 31.3 function can be passed as an argument to another function > def foo (xQuery YJFOC):. The return value of the function return func (XMagol y)... > foo (1Jing 2 and add) 31.4 can be a function > def bar ():. Return add... > func=bar () > func (1) 2) 3 two closure functions 2.1 closures and bundles
Based on the concept of function object, the function can be called anywhere, but the scope has been determined when the function is defined, regardless of the calling location of the function.
X=1def f1 (): def f2 (): print (x) return f2def f3 (): Xroom3 f2=f1 () # call F1 () return function f2 f2 () # need to execute according to the function definition, regardless of the position of the call f3 () # the result is 1
In other words, when a function is treated as data, it always follows its own scope. If the embedded function contains references to variables in the scope of the external function (rather than the global scope), then the 'embedded function' is a Closures function, or closure for short.
X=1def outer (): Xero2 def inner (): print (x) return innerfunc=outer () func () # result is 2
You can view the external variables wrapped by the closure function through the _ _ closure__ property of the function
> func.__closure__ (,) > func.__closure__ [0] .cell _ contents2
"closed" represents that the function is internal, and "package" represents the reference to the outer scope of the function. So no matter where the closure function is called, the variables that are wrapped around it are still used.
2.2 the purpose of closures
So far, we have got two ways to pass a value for the body of a function, one is to pass the value directly as a parameter, and the other is to package the value to the function.
Import requests# mode 1: def get (url): return requests.get (url). Text # mode 2: def page (url): def get (): return requests.get (url). Text return get
Tip: the requests module is used to simulate the browser to send a request to the website and download the page content locally, which needs to be installed in advance: pip3 install requests
Comparing the two methods, method one needs to pass in url repeatedly when downloading the same page, while method two only needs to pass a value once to get a closure function containing the specified url. There is no need to pass url to call the closure function later.
# download the same page get ('https://www.python.org')get('https://www.python.org')get('https://www.python.org')... # method 2 download the same page python=page ('https://www.python.org')python()python()python()...
This property of closure functions is sometimes called lazy computing. Using the method of wrapping values to functions will also be of great use in the next decorator
This is the end of the content of "how to use Python function objects and closures". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.