3

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?

1
  • 1
    You mean z = f ? Commented Jan 1, 2019 at 9:50

1 Answer 1

3

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
>>>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.