In math, for a given f(x), then we can assign a value y = f(3). This we can also do in Python.
However, in math we also regularly do something like defining a new function like z(x) = f(x). Is this possible in Python ?
In other words, given a defined function, can assign that function to another one?
Yes, that is possible. Not exactly as you have written but with a little change.
As in programming, the left hand side (L.H.S) of assignment should always be a variable not an expression like z(x).
So in place of writing z(x) = f(x) you can write z = f as follows.
Python functions are first class objects. Check https://www.geeksforgeeks.org/first-class-functions-python/.
>>> def f(v):
... return v ** 2
...
>>> y = f(3)
>>> y
9
>>>
>>> def f(x):
... return x ** 2
...
>>> y = f(3)
>>> y
9
>>>
>>> z = f # You can assume it as z(x) = f(x)
>>> y = z(3)
>>> y
9
>>>
>>> y = z(4)
>>> y
16
>>>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With