How does python view all variable names and values in the function body?
This article mainly explains the "python how to view the function body of all variable names and variable values", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in-depth, together to study and learn "python how to view the function body of all variable names and variable values" bar!
The specific usage is analyzed as follows:
The function of the python eval () function: evaluates the string str as a valid expression and returns the result.
Syntax: eval (source [, globals [, locals]])-> value
Parameter description:
Source: a code object returned by a Python expression or function compile ()
Globals: optional. Must be dictionary
Locals: optional. Any map object
If the globals parameter is provided, it must be of type dictionary; if the locals parameter is provided, it can be any map object.
The global namespace of python is stored in a dict object called globals (); the local namespace is stored in a dict object called locals (). We can use print (locals ()) to see all the variable names and values in the body of the function.
Examples of use:
In [1]: eval ("1 + 3") Out [1]: 4 In [2]: eval ("(1 + 2) * 3") Out [2]: 9 In [3]: eval ("[1,2,3]") Out [3]: [1,2,3] In [5]: eval ("type ({'name':' zhangsan'})") Out [5]: dict thanks for your reading The above is the content of "how python views all variable names and variable values in the function body". After the study of this article, I believe you have a deeper understanding of how python views all variable names and variable values in the function body, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!