Home > Backend Development > Python Tutorial > Python supports instance parsing of return functions

Python supports instance parsing of return functions

零到壹度
Release: 2018-04-03 10:47:05
Original
1706 people have browsed it

This article mainly introduces the example analysis of Python's support for return functions. The editor thinks it is quite good. Now I will share it with you and give it as a reference. Let’s follow the editor to take a look

I recently came into contact with python and saw the concept of returning functions in python. I have only been exposed to function return values ​​before. For example, python can return int, str, list, dict Waiting for type data, what I want to say here is that Python also supports return functions.

First look at the basic syntax of python that supports returning functions

def f():
    print 'call f()...'
    # 定义函数g:
    def g():
        print 'call g()...'
    # 返回函数g:
    return g
Copy after login

Here, the outermost function f will return a function g, which is the function g itself;
Let’s look at the function The calling process; (continued from above)

>>> x = f()   # 调用f()call f()...>>> x   # 变量x就是上面一个最外层函数返回的函数(不是函数值)<function g at 0x1037bf320>
>>> x()   # x指向函数,因此可以调用,这里就是调用内层函数x()的过程call g()...   # 调用x()就是执行g()函数定义的代码
Copy after login
  • Only the function of returning function:
    The returning function can delay the execution of some calculations. For example, if you define an ordinary summation function:

def calc_sum(lst):
    return sum(lst)
>>>calc_sum([1,2,3,4])
Copy after login

, the result is directly: 10

However, you can write code by returning the function idea, and you can "delay Calculation"

def calc_sum(lst):
    def lazy_sum():
        return sum(lst)    return lazy_sum
Copy after login

The following is the call:
Note that the following code does not execute the function to calculate the result, but returns the function.

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

The result is calculated when the returned function is called

>>>f()10
Copy after login

The following is an example:
Please write a function calc_prod(lst), which receives a list and returns a Function that returns a function that computes the product of its arguments.
Idea: First define a function that can calculate the product, and then return this function.

def calc_prod(lst):
    def lazy_prod():
        def f(x,y):
            return x*y        
        return  reduce(f,lst,1)    
return lazy_prod

f = calc_prod([1, 2, 3, 4])
print f()
Copy after login

                             

Related recommendations:

Return function

python return function/function programming

Some situations about function return values

The above is the detailed content of Python supports instance parsing of return functions. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template