InteractiveInterpreter runcode() in Python
With the help of InteractiveInterpreter.runcode()
method, we can only execute the pre-compiled source code having single or multiple lines by using InteractiveInterpreter.runcode()
method.
Syntax : InteractiveInterpreter.runcode(code)
Return : Return the result of executed source else error.
Example #1 :
In this example we can see that by using InteractiveInterpreter.runcode()
method, we are able to execute the piece of code and if run is successful we can get the result else error by using this method.
import code
from code import InteractiveInterpreter
source = 'print("GeeksForGeeks")'
compile_code = code.compile_command(source)
InteractiveInterpreter().runcode(compile_code)
|
Output :
GeeksForGeeks
Example #2 :
import code
from code import InteractiveInterpreter
source = 'a = 5; b = 5; li = [a * b for i in range(5)]; print(li)'
compile_code = code.compile_command(source)
InteractiveInterpreter().runcode(compile_code)
|
Output :
[25, 25, 25, 25, 25]
Last Updated :
26 Mar, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...