codeop.compile_command in Python
Last Updated :
22 Apr, 2020
With the help of codeop.compile_command()
method, we can compile the single or multiple lines of code to check for the syntax error if any by using codeop.compile_command()
method.
Syntax : codeop.compile_command(code)
Return : Return the object or compilation error if any.
Example #1 :
In this example we can see that by using codeop.compile_command()
method, we are able to compile the multiple lines of code by using this method.
from codeop import compile_command
code = 'a = 5 b = 9; print(a + b)'
compile_command(code)
|
Output :
a = 5 b = 9; print(a + b)
^
SyntaxError: invalid syntax
Example #2 :
from codeop import compile_command
code = '-a = 5'
compile_command(code)
|
Output :
SyntaxError: can’t assign to operator
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...