I have 2 functions as follows:
def test1(var):
return var*2
def test2(var):
return var*4
I want to pass a variable to form part of the code, something like below:
var='test2'
def new_test(var,4):
return var(4)
And I expect the output to be 16 (i.e. output from test2)
In excel, it is achievable via the function of =indirect(...). Is there any way to achieve that in Python?
asked Aug 27, 2017 at 5:17
user7786493
4734 gold badges7 silver badges14 bronze badges
1 Answer 1
Yes, instead of this:
var = 'test2'
def new_test(var, 4):
return var(4)
You can do this directly:
var = test2
def new_test(var, 4):
return var(4)
Functions are first class objects in Python.
answered Aug 27, 2017 at 5:28
wim
369k114 gold badges682 silver badges821 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-py
command=test2?