2

I have two files

test_def.py
def hi_test(a):
    return a

test_run.py

from test_def import hi_test
a = 'hi'
b = 'test'
c = 'lion'

run = "{0}_{1}".format(a, b)
run1 = run(c)
print run1

it is printing hi_test(lion) instead of executing / calling def function. can anyone help on this to execute the def function ?

1

3 Answers 3

3
import test_def
a = 'hi'
b = 'test'
c = 'lion'

run = "{0}_{1}".format(a, b)
run1 = getattr(test_def, run)(c)
print run1
Sign up to request clarification or add additional context in comments.

Comments

1

it can be archive by the following method.

import test_def
a = 'hi'
b = 'test'
c = 'lion'

run = "{0}_{1}".format(a, b)
run1 = getattr(test_def,run)
run2 = run1(c)
print run2

Comments

0

test_def.py

def hi_test(a):
    print a

test_run.py

from test_def import hi_test

a = 'hi'
b = 'test'
c = 'lion'

run = "{0}_{1}".format(a, b)
exec("%s('%s')"%(run, c))

Although, the first answer is better

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.