+ 2

Functions as Objects

def multiply(x, y): return x * y a = 4 b = 7 operation = multiply print(operation(a, b)) can anyone explain how it code works?

30th Apr 2018, 5:08 AM
Lakshman Patel
Lakshman Patel - avatar
3 Respostas
+ 2
multiply is a function that returns the multiplication of it's two arguments. operation is assigned to multiply and does the same thing now as multiply so it returns 7*4=28
30th Apr 2018, 5:13 AM
᠌᠌Code X
᠌᠌Code X - avatar
+ 1
functions are objects, thus, you can assign them to a variable. So calling operation is EXACTLY the same as multiply, with the same arguments.
30th Apr 2018, 5:12 AM
Amaras A
Amaras A - avatar
0
if I write operation = multiply then operation behave like multiply?
30th Apr 2018, 5:17 AM
Lakshman Patel
Lakshman Patel - avatar