PHP中文网2017-04-17 15:39:11
a,b,c = [1,2,3] Your count() function returns something like this [1,2,3], then a=1,b=2,c=3 , this is a simplified assignment
伊谢尔伦2017-04-17 15:39:11
This is python’s multi-finger return. The count() function returns a list of functions. There are three values in the list, namely f(1), f(2), f(3), which are assigned to the corresponding f1, f2, f3, and then executed the function when printing, and got 1, 4, 9
黄舟2017-04-17 15:39:11
count
is the function above
f1, f2, f3 = count()
uses three variables to receive the array returned by the function (directly receiving the elements in the array)
阿神2017-04-17 15:39:11
The count() function returns the fs list, which itself contains three items.
f1, f2, f3 = count()
means unpacking the list. Equivalent to
f1, f2, f3 = [1, 2, 3]
i.e., f1 = 1
, f2 = 2
, f3 = 3
The items in the original text are some closure functions.
ringa_lee2017-04-17 15:39:11
@sorashiro
Well, I am also a newbie. When I saw this code, there was something I didn’t understand. Please ask:
In the code r = f(i), why is the f(j) function not executed?
I followed the code in my head and got the answer: print: 1, 16, 81
The reason is that I thought that the code r = f(i) executed the f(j) function. I ran it on the computer and found that I was wrong. I didn’t understand this sentence: why r = f(i) didn’t What about executing the f(j) function?
大家讲道理2017-04-17 15:39:11
First answer what f1, f2, f3=count() means. Because the count() function returns a list, Python allows you to assign the list to multiple variables, as long as the length of the list is equal to the number of variables.
Secondly, let’s talk about why it is 1, 4, 9. This is related to the closure of the function. Simply put, the internal function has a memory function, and it remembers the parameters given to it by the external function.
Therefore, f1, f2, and f3 are not executed immediately after receiving the list. After f1(), f2(), and f3() are called, they will all execute j*j at the same time.