As a sort of exercise, I would like to try running a program on both python2 and python3. Here is the program:
def enter_name():
name = raw_input('Please enter your name: ')
print("Your name is: %s" % name)
Of course the above is written in python2
as in python3
raw_input
is changed to input
. I would like to write a wrapper that allows this python2
code to run in python3
. It would modify the source code to change the "raw_input" to "input". And would run like this:
@run_python3
def enter_name():
name = raw_input('Please enter your name: ')
print("Your name is: %s" % name)
And the decorator would convert the source code for the function raw_input to input if in a python3*
environment. This is related to my previous question: Is it possible to modify a function's code?. How could the above be done using a decorator (not something like 2to3
)?