return functions in Python

Source: Internet
Author: User

Python's functions can not only return data types such as int, str, list, dict, but also return functions!

For example, to define a function f (), we let it return a function g, which can be written like this:

def f ():     Print ' Call f () ... '    # define function g:    def g ():         Print ' Call g () ... '    # return function g:    return G

Looking closely at the function definition above, we define a function g inside the function f. Since the function g is also an object, the functions of the name G is the variable that points to the function g, so the outermost function f can return the variable g, which is the function G itself.

Call the function f, and we'll get a function of the F return:

>>> x = f ()   #  calls F () callf () ... >>> x   #  variable x is the function returned by F ():<function G at 0x1037bf320>>>> X ()   # x points to the function, so call g ()   can be called ... # calling X () is the code that executes the G () function definition

Note that the return function and return value are distinguished:

def myabs ():      return ABS   #  returns function def  MYABS2 (x):    return ABS (x)   #  Returns the result of the function call, the return value is a number

The return function can delay execution of some computations. For example, if you define a normal sum function:

def calc_sum (LST):     return sum (LST)

When the Calc_sum () function is called, the result is calculated and obtained immediately:

>>> Calc_sum ([1, 2, 3, 4])10

However, if you return a function, you can "defer calculation":

def calc_sum (LST):     def lazy_sum ():         return sum (LST)     return Lazy_sum

# call Calc_sum () does not calculate the result, but returns the function:

>>> f = calc_sum ([1, 2, 3, 4])>>> F<function lazy_sum at 0x1037bfaa0>

# The result is calculated when a call is made to the returned function:

>>> F ()10

Since the function can be returned, we can decide in the subsequent code whether or not to invoke the function.

Practice:

Write a function Calc_prod (LST) that receives a list, returns a function, and returns a function that calculates the product of the parameter.

def Calc_prod (LST):     def cal ():         return reduce (lambda x,y:x*y,lst)    return= Calc_prod ([1, 2, 3, 4 ])print f ()

Another method returns the product of the elements in the list:

def Calc_prod (LST):     def cal ():         return map (Lambda x:x*x,lst)    return= Calc_prod ([1, 2, 3, 4  ])print f ()

Results:

24

The map function returns the result:

[1, 4, 9, 16]

return functions in Python

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.