[Tutor] making a string

Steven D'Aprano steve at pearwood.info
Mon May 27 02:20:25 CEST 2013


On 27/05/13 07:40, Jim Mooney wrote:

> Good to know that compile doesn't check syntax, since I erroneously
> thought it did.

compile does check syntax.


py> compile("23 = 43", "", "exec")
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
   File "", line 1
SyntaxError: can't assign to literal


The compile built-in function does the same as the Python compiler that compiles your code before you run it. It has three modes:

- "exec", which operates in the same way that Python compiles a module;

- "single", which operates in the same way that Python compiles a single statement in the interactive interpreter; and

- "eval", which tells Python that the code must be a single expression and not a statement or a block of code.



-- 
Steven


More information about the Tutor mailing list