Python compile() Funksie
Voorbeeld
Stel teks saam as kode, en voer dit uit:
x = compile('print(55)', 'test', 'eval')
exec(x)
Definisie en gebruik
Die compile()
funksie gee die gespesifiseerde bron terug as 'n kode-objek, gereed om uitgevoer te word.
Sintaksis
compile(source,
filename, mode, flag, dont_inherit,
optimize)
Parameterwaardes
Parameter | Description |
---|---|
source | Required. The source to compile, can be a String, a Bytes object, or an AST object |
filename | Required. The name of the file that the source comes from. If the source does not come from a file, you can write whatever you like |
mode | Required. Legal values: eval - if the source is a single expression exec - if the source is a block of statements single - if the source is a single interactive statement |
flags | Optional. How to compile the source. Default 0 |
dont-inherit | Optional. How to compile the source. Default False |
optimize | Optional. Defines the optimization level of the compiler. Default -1 |
Meer voorbeelde
Voorbeeld
Stel meer as een stelling saam en voer dit uit:
x = compile('print(55)\nprint(88)', 'test', 'exec')
exec(x)