CKTIS / Python
compile()
Kompiluje tekst, bajty lub drzewo AST dla wybranego trybu.
Wersja: Python 3.14; repository image, production runtime not attested
Składnia
compile(source, filename, mode, flags=0, dont_inherit=False, optimize=-1)Kompiluje tekst, bajty lub drzewo AST dla wybranego trybu.
eval wymaga wyrażenia, exec przyjmuje instrukcje, a single służy do instrukcji interaktywnej. Nazwa pliku opisuje błędy. Skompilowane mnożenie po wykonaniu daje 28. W trybie eval instrukcja x = 4 powoduje SyntaxError, a wyrażenie (x := 4) daje 4.
Flaga PyCF_ONLY_AST zwraca drzewo.
code = compile("x + 1", "<calc>", "eval")
assert code.co_filename == "<calc>" and eval(code, {"x": 3}) == 4
assert eval(code, {"x": 9}) == 10
Przykłady
expression = compile("4 * 7", "<example>", "eval")
print(eval(expression))
try:
compile("x = 4", "<example>", "eval")
except SyntaxError as error:
print(type(error).__name__)
Wynik
28 SyntaxError
Flaga PyCF_ONLY_AST zwraca drzewo.