+ 1
Functions assigned and reassigned to variables, and later referenced by those names: doubt
======================================= def multiply(x, y): return x * y a = 4 b = 7 operation = multiply print(operation(a, b)) ============================= The code above is an example given in the "Functions as objects" lesson. My question is: what exactly is "operation"? Is it a variable? Is it a pointer? Did it duplicated the multiply function? I know that I can treat it as a normal variable and do something like operation = 22 print (operation) >>>22 and as expected, print (operation(a, b)) no longer works. Thanks in advance!
2 Réponses
+ 2
It is a variable that references the existing function.
+ 1
Imo, in pythin, variables only contain pointers. For example,
>>>a = [1, 2]
>>>b = a
>>>a[0] = 3
>>>print(b)
[3, 2]
this happens. I think it looks like how pointer work in C.