Python-fullstack-s13-day11-python Foundation

Source: Internet
Author: User
Tags closure

Application of Function name

A function name is a variable, but it is a special variable that can be executed in conjunction with parentheses.
1. memory address of Function name

1 def func (): 2     Print (" hehe ") 3 Print # function func at 0x02969c90

2. Function name can be assigned to other variables

def func ():     Print (" hehe ") Print  #  assign a function as a variable to another variable a ()    #  function call func ()

3. Function names can be used as elements of a container class

1 deffunc1 ():2     Print("HAHA1")3 defFunc2 ():4     Print("HAHA2")5 deffunc3 ():6     Print("HAHA3")7 defFunc4 ():8     Print("HAHA4")9LST = [Func1,func2,func3,func4]#list memory is the memory address of the functionTen  forIinchLST: OneI ()

4. Function name can be used as parameter of function

 def   func ():  print  ( "  eat  "  )  def   Func2 (FN):  print  ( "  I am func2  "  ) FN ()  #   Perform the FN passed over  print  ( "  I'm func2   " ) Func2 (func)  #   function func as parameter to pass FUNC2 parameters fn  

5, function name can be used as the return value of the function

deffunc1 ():Print("here is the function 1")    defFunc2 ():Print("here is the function 2")    Print("here is the function 1")    returnFUNC2FN= Func1 ()#Execute function 1, function 1 returns the function 2, when FN points to the above function 2FN ()#perform the functions returned above

Second, closed package

What do you mean, closures? A closure is an inner function, a reference to a variable of an outer layer function (non-global), called a closed packet

def func1 ():     " Alex "    def Func2 ():       Print # Closed Package     Func2 () func1 ( )

Question: The closure in class is to let memory remember a variable? function execution Complete can you remember this variable in Houhai?

We can use __CLOSURE__ to detect whether a function is closed, use the name __closure__ to return a cell is a closure, return none is not a closure

def func1 ():     " Alex "    def Func2 ():         Print # Closed Package     Func2 ()    print(func2.  __closure__#  (<cell at 0x03634b90:str object at 0x03424980>,)func1 ()  

Question, how do I adjust the outside function?

def outer ():     " Alex "    # intrinsic functions    def inner ():         Print (name)     return  # Access external function, get function address to intrinsic function #  access intrinsic function

# What if multiple layers are nested? It's simple, just a layer of layers to go back to the outer layer.

def func1 ():     def Func2 ():         def func3 ():             Print (" hehe ")         return func3     return func2func1 () () ()

Examples of closures: (Web crawler Applications!) )

 fromUrllib.requestImportUrlopendefBut (): Content= Urlopen (""). Read ()defget_content ():returncontentreturnGET_CONTENTFN= but ()#It's time to start loading the 100.#What 's the need? Do not need to be in the practice of the content? Often time-consuming, network connection operation.Content = fn ()#Get contentPrint(content) Content2= FN ()#Get content backPrint(Content2)

Third, the first knowledge of the decorative device

Write code opening and closing principle: Open the function extension, modify the code closure. You can add functionality to the original code, but you cannot modify the code

Adorner version

defcreate_people ():Print("Nu wa is very powerful, pinch a clay figurine to blow the tone will become a person! ")#create_people ()defWater (FN):definner ():Print("watering first .") fn ()Print("Fertilization")    returnInner#func = water (create_people)#func ()Create_people = Water (create_people)#function names, like variable names, can be assigned to other variablesCreate_people ()

Version with a syntactic sugar

@waterdefcreate_people ():Print("Nu wa is very powerful, pinch a clay figurine to blow the tone will become a person! ")#create_people ()defWater (FN):definner ():Print("watering first .") fn ()Print("Fertilization")    returnInner#func = water (create_people)#func ()#create_people = Water (create_people) # function name and variable name, can be assigned to other variablesCreate_people ()

Note: It can be understood that a function is re-written, which wraps the original function in the top

That is, the function that needs to add function is wrapped in def/def inner code block

Python-fullstack-s13-day11-python Foundation

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: 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.