Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the five steps that Python actually takes to execute the code?

Shulou Source: shulou.com Published: 2022-06-02 23:27:08 09月19日 Update

What are the five actual steps of Python code execution? I believe many inexperienced people are at a loss about this. Therefore, this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Python executes the code through the following process:

1. Tokenizer performs lexical analysis and decomposes the source program into Token.

2. Parser creates CST based on Token

3. CST is converted to AST

4. AST is compiled into bytecode

5. Execution bytecode

When executing Python code, for example, if the code is stored in a file, Python will call the PyParser_ASTFromFile function to convert the code contents of the file to AST:

Mod_ty

PyParser_ASTFromFile (FILE * fp, const char * filename

Int start, char * ps1

Char * ps2, PyCompilerFlags * flags, int * errcode

PyArena * arena)

{

Mod_ty mod

Perrdetail err

Node * n = PyParser_ParseFileFlags (fp, filename

& _ PyParser_Grammar

Start, ps1, ps2, & err, PARSER_FLAGS (flags))

If (n) {

Mod = PyAST_FromNode (n, flags, filename, arena); PyNode_Free (n)

Return mod

}

Else {

Err_input & err)

If (errcode)

* errerrcode = err.error

Return NULL

}

}

In

PyParser_ParseFileFlags

After the file is converted to CST in the Python execution code, the PyAST_FromNode function converts CST to AST. This function is defined in include\ ast.h:

PyAPI_FUNC (mod_ty) PyAST_FromNode (const node *, PyCompilerFlags * flags, const char *, PyArena *); after reading the above, have you mastered what are the five steps of Python's actual execution of the code? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Tags: Code content function file actual step byte method more problem helpless for this reason situation skill time source program article experience Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Linux macOS Huawei Shulou Technology