Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”

This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.

The 25th official Python column, stop! Don’t miss this zero-based article!

Today we’ve been writing code, we’ve written a lot, but this time we’re going to talk about functions in Python

What is a function

There are functions in every language, even in The Excel that you use, there are functions in the math that we used to study and there are all kinds of functions.

Functions in Python are the same.

Def f(x): print(" ",x) return xCopy the code

This function, y = f(x), is mathematically expressed as a line with slope 1.

Nested calls to a function

Def z(x): pass def f(x): print(" ",x) return z(x)Copy the code

Like this, we call the z (x) function in f(x) (using the pass keyword, for demonstration purposes only)

Can we define one function and call another function without defining z of x?

It’s like implementing the square of a number, the square of a function, something like that.

Higher-order functions

def f(z):
    return z()
Copy the code

This is a higher-order function, f requires an argument, and the argument has to be a function.

When we use f(z), we can’t give a value like f(2), f(3). Or if a function such as d(x) returns a non-function result, we cannot call it like this :f (d(1)).

The committee prepared the following code to evolve from a simple function to a higher-order function:

#! /usr/bin/env python # -*- coding: utF-8 -*- # @author: xuewei # @csdn /Juejin/Wechat: # @XueWeiTag: CodingDemo # @Project: hello def f1(x): return x def f2(x, z): return x + z / 10 def f3(x, z=100, *dynamic_args): sum = 0 for arg in dynamic_args: Sum += arg return x + z / 10 + sum / 10000.0 def dummy_sum(*args): return 0 def f4(x, z=100, sum += arg): Return x + z / 10 + sum_func() / 10000.0 print(f1(100)) print(f2(100, z=50)) print(f3(100, 50, 4, 5) 6)) def sum_g(*dynamic_args): def sum_func(): sum = 0 for arg in dynamic_args: sum += arg return sum return sum_func print(f4(100, 50, sum_g(4, 5, 6)))Copy the code

Here we see the functions F1, F2, F3, f4.

*dynamic_args is a dynamic parameter with an indefinite length. So F3 clearly declares 3 parameters, and we give 5 parameters. Here F3 thinks x=100, z=50, dynamic_args = [4, 5, 6]

Let’s look at the output first:

F3 and F4 look the same.

But the nature is completely different. The reader can think about it for ten seconds.

F4 is very elastic because the third parameter is a function.

Higher-order functions can help us reduce the dimensions of our calculations (three to two, two to one).

Let’s think about calculating the area of a circle and a square

I’m sure you can write the following two functions with your eyes closed:

#! /usr/bin/env python # -*- coding: utF-8 -*- # @author: xuewei # @csdn /Juejin/Wechat: # @XueWeiTag: CodingDemo # @File: func_demo2.py # @Project: hello import math def circle_area(r): return math.pi * r * r def rectangle_area(a, b): return a * bCopy the code

Here’s the mathematical formula for the area of a circle:


f ( r ) = PI. r 2 f(r) = \pi * r^2

Here is the mathematical formula for the area of a rectangle:


f ( a . b ) = a b f(a, b) = a * b

We see here that some of them have one argument, some of them have two how do they become higher order functions?

The reader can think about it for a moment.

Here’s the code:

#! /usr/bin/env python # -*- coding: utF-8 -*- # @author: xuewei # @csdn /Juejin/Wechat: # @XueWeiTag: CodingDemo # @File: func_demo2.py # @Project: hello import math def circle_area(r): return math.pi * r * r def rectangle_area(a, b): return a * b def area(x, linear, factor): return x * linear(x, factor) def relation(x, factor): Return x * factor a = 10 b = 20 print(" rectangle_area: ", rectangle_area(a, b)) print(" rectangle_area: ", circle_area(a)) print(" rectangle_area: ", rectangle_area(a, b)) print(" rectangle_area: ", circle_area(a)) print(" rectangle_area: ", rectangle_area(a)) ", area(a, relation, factor=b/a)) print(", area(a, relation, factor=math.pi)Copy the code

The results are shown below:

This is just one solution.

As you can see from the code, we treat both circles and rectangles as the square of some reference (radius/edge) multiplied by a coefficient.

Next, we add the square area calculation:

#! /usr/bin/env python # -*- coding: utF-8 -*- # @author: xuewei # @csdn /Juejin/Wechat: # @XueWeiTag: CodingDemo # @File: func_demo2.py # @Project: hello import math def circle_area(r): return math.pi * r * r def square_area(a): return a * a def rectangle_area(a, b): return a * b def area(x, linear, factor): return x * linear(x, factor) def relation(x, factor): Return x * factor a = 10 b = 20 print(" rectangle_area: ", rectangle_area(a, b)) print(" rectangle_area: ", square_area(a)) print(" rectangle_area: ", square_area(a)) print(" rectangle_area: ") ", circle_area(a)) print(" rectangle area: ", area(a, relation, factor=b/a)) print(" rectangle area:" ", area(a, relation, factor=1)) print(", area(a, relation, factor=math.pi)Copy the code

The above code executes as follows:

And that’s the magic of higher-order functions, so let’s think about it in terms of squares.

Using only one area function and relation function, neither of which need to be modified, just give a factor (empirical factor), you can quickly calculate its area.

Why do higher-order functions reduce dimensions

From the distance above as a function of the area, we can see that we can view both circles and rectangles as a one-dimensional function.

Then the square area is used as a reference to quickly estimate the area of the circle and square.

Factor = math.pi / 4

It will feel more like it.

conclusion

In addition to the functions described above, parameters, higher-order functions. We can also use lambda functions:

Lambda argument 1, argument 2... The NTH argument: evaluates the expressionCopy the code

The relation function above can be omitted, and the final call is changed to:

Print (a, lambda x, f: x * f, factor=b/a) print(a, lambda x, f: x * f, factor=b/a) X * f, factor=1) print(" x * f: ", area(a, lambda x, f: x * f, factor=math.pi)Copy the code

By the way, if you like Python, please check out the Committee’s Python Basics column or Python Getting Started to Master column

Continuous learning and continuous development, I am Lei Xuewei! Programming is fun. The key is to get the technology right. Welcome to wechat, like support collection!