Python learning the application of D11 function name (first object) closure adorner

Source: Internet
Author: User
Tags closure

02, the application of the function name (the first object).
# 1, the direct print function name gets the memory address of the function <function func1 at 0x0000000002876b70>
# Print (FUNC1)
#2, the function name can be assigned a value operation.
# def func1 (): #      Print (666)#  f1 = func1#  F1 ()
# 3, the function name can be used as a function parameter.
# def func1 (): #      Print (666)# #  def func2 (x):#     x ()#      Print (555)#  func2 (func1)

# 4, the function name can be used as an element of the container class data type.
#def func1 ():#print (666)##def func2 ():#Print (222)##def func3 ():#Print (111)##def func4 ():#Print (777)#L1 = [Func1, Func2, func3, Func4]#For i in L1:#I ()#Dic1 = {#1:func1,#2:func2,#3:func3,#4:func4,# }#Dic1[1] ()

# 5, the function name can be used as the return value of the function
def func1 ():     Print (666) def Func2 (x):  #  x = func1    print(222)      return= Func2 (func1) ret ()



03, closures.
  # inner layer function A reference to a non-global variable outside a layer is called a closure   
#判断是不是闭包 function name. __closure__
# The None returned is not a closure , the cell is returned .... What is the use of closures
# closures?
#当执行一个函数时 If the interpreter determines that the internal closure of this function exists, so that
#Python就一个机制, the temporary namespace in which the closure resides does not disappear as the function finishes executing.
 #   def func1 ():  #   name = ' old boy '  ##   def Inner ():  #   print (name)  #   inner ()  #   print (inner.__closure__) # <cell at 0x000000000282a768:str object at 0x000000000285 6e10>  ##   func1 ()  
 #   def func1 ():  #   global name  #   name = ' old boy '  #   def Inner ():  #   print (name)  #   inner ()  #   print (inner.__closure__) # None  #   func1 ()  
# def func1 (x):   # x = ' old boy '#     def inner ():#         print (x)#       inner ()#     print (inner.__closure__)   # (<cell at 0x00000208229eee88:str object at 0x0000020822d436f0>,)#  name = ' old boy '#  FUNC1 (name)

deffunc ():deffunc1 (): Name="old boy"    defFunc2 (): nonlocal name Name="Alex"    deffunc3 ():GlobalName Name="Taibai"name="Day Day"func1 ()Print(name)#1st DaysFunc2 ()Print(name)#2,alexfunc3 ()Print(name)#3,alexfunc ()Print(name)#4, Taibai
# From urllib.request import Urlopen ## def index (): #      url = "http://www.xiaohua100.cn/index.html"#     def get ():#          return Urlopen (URL). Read () #      return get# #  Xiaohua = index ()#  content = Xiaohua ()  # #  print (content)


04, Adorner.

#装饰器功能: Add some additional functions to the original function, log, login registration, and so on, without changing the original function.


Test the efficiency of the program
Import Time" "first version, test function Low" "#def login ():#Time.sleep (0.3)#print (' wash more healthily ... ')##def timmer ():#start_time = Time.time ()#Login ()#end_time = Time.time ()#print (' Execution time of this function%s '% (end_time-start_time))#Timmer ()

#changed the way I used to execute the function, not good#def login ():#Time.sleep (0.3)#print (' wash more healthily ... ')## login ()##def Register ():#Time.sleep (0.4)#print (' wash healthier 22222 ... ')## Register ()#def Timmer (f):#start_time = Time.time ()#f ()#end_time = Time.time ()#print (' Execution time of this function%s '% (end_time-start_time))##Timmer (login)#Timmer (Register)

#Although the way to execute the function has been infinitely close to the original way, but more cumbersome, added two-step code. Change#def login ():#Time.sleep (0.3)#print (' wash more healthily ... ')## login ()##def Timmer (f):#start_time = Time.time ()#f ()#end_time = Time.time ()#print (' Execution time of this function%s '% (end_time-start_time))##f1 = login # gives the login function name to F1#login = Timmer # The Timmer function name was given to login#Login (F1) # Timmer (login)

#Starter Decorators#def login ():#Time.sleep (0.3)#print (' wash more healthily ... ')## login ()##def Timmer (f): # f = Login function name##def inner ():#start_time = Time.time ()#f () # login ()#end_time = Time.time ()#print (' Execution time of this function%s '% (end_time-start_time))#return inner##login = Timmer (login) # Inner This login is a new variable#login () # inner ()#name = ' Alex '#name = ' old boy '

#simple version decorator syntax sugar#def Timmer (f): # f = Login function name#def inner ():#start_time = Time.time ()#f () # login ()#end_time = Time.time ()#print (' Execution time of this function%s '% (end_time-start_time))#return inner##@timmer # login = Timmer (login) # Inner This login is a new variable#def login ():#Time.sleep (0.3)#print (' wash more healthily ... ')#Login ()#@timmer # Register = timmer (register)#def Register ():#Time.sleep (0.2)#print (' wash more healthily ... ')



Python learning the application of D11 function name (first object) closure adorner

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.