+ Decorator closure function (the eleventh day)

 

the eleventh day

Closure function


 

Closure function is nested functions, function objects, and the scope of the name space binding body

Direct parameter passing # 
# DEF FUNC (X):
# Print (X)
#
# FUNC (100) # parameter passing through the closure function # DEF foo (NUM): # NUM = 100 # DEF COO (): # Print ( NUM) # return COO # FUNC = foo (1000) # FUNC ()








Application package closure function


Learn: In order to prepare decorator

Download: pip3 install requests

Use: import requests


Requirements: crawling a website, print length of the data acquisition

# 直接传参
import requests

# def sp_fu(url):
#     response = requests.get(url)
#     if response.status_code == 200:
#         print(len(response.text))
#         print(response.text)
# url = 'https://www.cnblogs.com/xiaoyuanqujing/'
# sp_fu(url)


# def spider_outer(url):
#     # url = 'https://www.cnblogs.com/xiaoyuanqujing/'
#     def spider_inner():
#         response = requests.get(url)
#         if response.status_code == 200:
#             print(len(response.text))
#     return spider_inner
#
# spider_blog = spider_outer('https://www.cnblogs.com/xiaoyuanqujing/')
# spider_blog()

Decorator *

'' ' 
Needs: Statistics of time to download movies decorator:   You can not modify the source code of decorative objects   does not modify the way is called decorative objects   decorated objects: ---> need to add function function   decorators: ---> new features are added decorative objects function '' ' # Import time # DEF download_movie (): # Print (' to start downloading movies ... ') # # simulate movie download time 3 seconds # time.sleep (3) # wait 3 seconds # print ( 'successful movie download ...') # # DEF time_record (FUNC): # DEF Inner (): # start_time = time.time () # FUNC () # end_time = time.time () # Print (f 'elapsed time: {end_time - start_time}' ')#     # return inner# download_movie = time_record(download_movie)# download_movie()

























# Was decorated object has a return value
# DEF download_movie ():
# print ( 'to start downloading movies ...')
# # simulate movie download time 3 seconds
# time.sleep (3) # wait 3 seconds
# print ( 'movie download successful ... ')
# return' Ozawa .mp4 ' # DEF foo (): # download_movie A = () # A return # Print (download_movie ()) # DEF time_record (FUNC): # Inner DEF ( ): # = START_TIME the time.time () # RES = FUNC () # = END_TIME the time.time () # Print (F 'time consuming: END_TIME {-} START_TIME') # return RES # return Inner # download_movie = time_record (download_movie) # download_movie ()



















Decorative object has parameters to be #
# DEF download_movie (URL): # Print (F '{} in the URL to download the movie starts ...') # analog movie download time of 3 seconds # time.sleep (3) # wait 3 seconds # print ( 'successful movie download ...') # return 'Ozawa .mp4' # DEF time_record (FUNC): # FUNC <- download_movie # # # url = 'https://www.baidu.com/' # # # in a closure function # Inner DEF (URL): # # start statistics # START_TIME the time.time = () # RES = FUNC (URL) FUNC # (URL) ---> download_movie (URL) ## when function executes statistics after completion, obtain the current time # END_TIME the time.time = () # # end statistics, statistical time print # print (f 'elapsed time: {end_time - start_time}')#         return res#     return inner#
   
   


















#
# = Download_movie time_record (download_movie)
#
# # download_movie (URL) -> Inner (URL)
# download_movie ( 'https://www.baidu.com') # # DEF download_movie2 (* args, ** kwargs): # Print (F '{} in the URL to download the movie starts ...') # # analog movie download time of 3 seconds # time.sleep (3) # wait 3 seconds # Print ( "movie download successful .. . ') # return' Ozawa .mp4 ' # DEF time_record (FUNC): FUNC # <- download_movie # = # URL' https://www.baidu.com/ ' # # # in a closure function Inner DEF # (* args, ** kwargs): # # statistical begin # time.time start_time = () #




















FUNC RES = # (* args, ** kwargs) # FUNC (url) ---> download_movie (url)
#
# # When the function is executed which statistics are completed, to get the current time
# end_time = time.time ()
# # statistics ends, the print time statistics
# print (f 'elapsed time: {END_TIME - START_TIME}')
# return RES
# Inner return
#
#
# download_movie = time_record (download_movie)
#
# # download_movie (URL) -> Inner (URL)
# download_movie ( 'https://www.baidu.com')

 

Guess you like

Origin www.cnblogs.com/zhangjinyi97/p/11844845.html