Step two of the Python function

Source: Internet
Author: User

The essence of Function name

The function name is essentially the memory address of the function.

1, can be used as a value or variable reference

def func ():    print (111) print (func) #<function func at 0x00000218a911ab70>--function in memory address F = Func    # Pass it to the variable fprint (f)    #<function func at 0x00000279a5a6ab70>f ()         #111

2. Functions can be stored as elements of a container type

#将其当做容器存储在列表中def func1 ():    print (' func1 ') def func2 ():    print (' Func2 ') def func3 ():    print (' func3 ') lis = [ Func1, Func2, Func3]lis[0] ()      #func1lis [1] ()      #func2lis [2] ()      #func3

3, can be used as a function parameter and return value

#当做参数和返回值def func1 ():    print (' func1 ') def func (argv):    argv ()              return argv     #func1作为返回值f = func (func1)     #func1作为参数f () #输出func1func1

Summarize:

# 1. The memory address of the function name is--print (letter name). # 2, the function name can be assigned to other variables. # 3. The function name can be used as the element of the container class. # 4, the function name can be used as a function parameter. # 5, the function name can be used as the return value of the function. # Scientific Name: First Class objects (first-class object)
# In short: You can use the function name as a variable

Second, closed package

An inner function is defined in an outer function, and a temporary variable of the outer function is used in the inner function, and the return value of the outer function is a reference to the inner function. This makes up a closure (i.e., a reference to a variable of the inner layer function, the outer layer function (non-global)).

#闭包函数的基本用法def func ():    name = ' Eva '    def Inner ():        print (name)    return innerf = Func () f ()         #eva

Common properties for detecting closure functions: __closure__

#闭包函数 # Output If a cell is the closure function Def wrapper ():    name = ' is closure '    def inner ():        print (name)             #是闭包    inner ()    Print (inner.__closure__)    # (<cell at 0x000001945ce07588:str object at 0x000001945ce06b70>,) wrapper () # Non-closure function # Output If None is not the closure function name = ' not closures ' Def wrapper ():    def inner ():        print (name)             #不是闭包    inner ()    print (inner.__closure__)    #Nonewrapper ()

Step two of the Python function

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.