HomeLanguagesInteractiveInterpreter runcode() in Python

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 and InteractiveInterpreter
import code
from code import InteractiveInterpreter
  
source = 'print("GeeksForGeeks")'
compile_code = code.compile_command(source)
  
# Using InteractiveInterpreter.runcode() method
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)
  
# Using InteractiveInterpreter.runcode() method
InteractiveInterpreter().runcode(compile_code)


Output :

[25, 25, 25, 25, 25]

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32688 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
7057 POSTS0 COMMENTS
Nicole Veronica
12198 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12293 POSTS0 COMMENTS
Shaida Kate Naidoo
7182 POSTS0 COMMENTS
Ted Musemwa
7447 POSTS0 COMMENTS
Thapelo Manthata
7160 POSTS0 COMMENTS
Umr Jansen
7150 POSTS0 COMMENTS