Function objects for advanced python tutorials (functions are also objects)

  • 2020-04-02 14:02:52
  • OfStack

With the idea that everything is an object, let's go back to functions again. A function is also an object with properties (you can use dir() queries). As an object, it can also be assigned to other object names, or passed as an argument.

Lambda functions

Before we expand, let's mention lambda functions. You can use the syntax of lambda functions to define functions. The lambda example is as follows:


func = lambda x,y: x + y
print func(3,4)

Lambda generates a function object. The function takes x,y, and returns x+y. The function object is assigned to func. Func is called just like a normal function.

The above definition can be written in the following form:


def func(x, y):
    return x + y

The function is passed as an argument

A function can be passed as an object with arguments. The function name (such as func) is the object. Such as:


def test(f, a, b):
    print 'test'
    print f(a, b) test(func, 3, 5)

The first argument to the test function, f, is a function object. Pass func to f, and f() in test has the function func().

We can therefore improve the flexibility of the program. You can use the test function above to take different function arguments. Such as:


test((lambda x,y: x**2 + y), 6, 9)

The map () function

Map () is Python's built-in function. Its first argument is a function object.


re = map((lambda x: x+3),[1,3,5,6])

Here, map() takes two arguments, a function object defined by lambda and a table with multiple elements. The function of map() is to apply the function object to each element of the table in turn, with the result of each action stored in the returned table re. Map operates on the data by reading in the function (lambda in this case) (where the "data" is each element in the table, and the "operation" is to add 3 to each data).

In Python 3.x, the return value of map() is a loop object. You can use the list() function to transform the loop object into a table.

If the function object as an argument has multiple arguments, you can pass multiple arguments of the function argument to map() in the following way:


re = map((lambda x,y: x+y),[1,2,3],[6,7,9])

Map () takes one element at a time from each of the two tables and brings it into the function defined by lambda.

The filter () function

The first argument to the filter function is also a function object. It also applies a function object as an argument to multiple elements. If the function object returns True, the next element is stored in the returned table. Filter filters the data by reading in the function. Likewise, in Python 3.x, the filter returns not a table, but a loop object.

The use of the filter function is as follows:


def func(a):
    if a > 100:
        return True
    else:
        return False print filter(func,[10,56,101,500])

To reduce () function

The first argument to the reduce function is also a function, but there is a requirement that the function itself can accept two arguments. Reduce can be applied progressively to various parameters. The following cases:


print reduce((lambda x,y: x+y),[1,2,5,7,9])

The first argument to reduce is the lambda function, which takes two arguments x,y, and returns x+y.

Reduce passes the first two elements (1 and 2) in the table to the lambda function, and you get 3. This return value (3) will be used as the first argument to the lambda function, and the next element in the table (5) will be used as the second argument to the lambda function, making the next call to the lambda function and getting 8. Lambda functions are called in turn, each time the first argument to the lambda function is the result of the previous operation, and the second argument is the next element in the table, until there are no remaining elements in the table.

So this is the same thing as (1+2)+5)+7)+9

According to mmufhy's warning: the reduce () function is not directly used in 3.0, it is defined in the functools package, and the package needs to be introduced, see the comments section.

conclusion

A function is an object

Define the function in terms of lambda

The map ()

The filter ()

Reduce ()


Related articles: