Skip to content
Related Articles

Related Articles

Improve Article

code.compile_command() in Python

  • Last Updated : 22 Apr, 2020

With the help of code.compile_command() method, we can compile the single or multiple lines of code to check for the syntax error if any by using code.compile_command() method.

Syntax : code.compile_command(code)
Return : Return the object or compilation error if any.

Example #1 :
In this example we can see that by using code.compile_command() method, we are able to compile the multiple lines of code by using this method.




# import code
from code import compile_command
  
code = 'a = 5 b = 9; print(a + b)'
# Using code.compile_command() method
compile_command(code) 

Output :

a = 5 b = 9; print(a + b)
^
SyntaxError: invalid syntax



Example #2 :




# import code
from code import compile_command
  
code = '-a=5'
# Using code.compile_command() method
compile_command(code) 

Output :

SyntaxError: can’t assign to operator

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :