Python built-in function -compile()

Python's built-in function compile()--compile.

**What's the use of this function? One of the simplest examples is our code, which will be read by the interpreter. The interpreter reads it as a string, and then it is compiled by compile and converted into python-recognizable code. .So python can be executed. **

**Here is an example, which is to compile a string of str into python code through compile. The details are as follows: **

Reference from http://www.cnblogs.com/wupeiqi/p/4592637.html

Execute a function represented by a string and provide global variables for the function##

The content of this article can be seen from the title, which is to prepare for the subsequent analysis of the tornado template. It is also due to the clever use of this knowledge point, so I will use a separate article to introduce it. Not much nonsense, just go to the code:

#! usr/bin/env python
# coding:utf-8
  
namespace ={'name':'wupeiqi','data':[18,73,84]}
  
code ='''def hellocute():return  "name %s ,age %d" %(name,data[0],) '''
  
func =compile(code,'<string>',"exec")
  
exec func in namespace
  
result = namespace['hellocute']()
  
print result

The execution result of this code is: name wupeiqi, age 18

Analysis of the above code:

This code is used very cleverly and blindly. It turns the string into a function and also provides global variables for the function. For this function, it is the most important part of the template language part of the python web framework, because in the template processing process, the html file is first read, then the html file is divided, and then the divided files form a string representation The function, and then the function that uses the above method to perform the string representation.

Recommended Posts

Python built-in function -compile()
Python3 built-in function table.md
Python enumerate() function
Python function buffer
Python3 built-in module usage
Python custom function basics
Join function in Python
Python function basic learning
Python data analysis-apply function
Python Print print timer function
Python defines a function method
Python high-order function usage summary!
Python realizes online translation function
ubuntu18.04 compile and install python3.8
Python tornado upload file function
Python magic function eval () learning
How Python implements FTP function
CentOS 6.9 compile and install python
CentOS 6 compile and install python 3
Python implements image stitching function
Python high-order function usage summary!
Python telnet login function implementation code
Python| function using recursion to solve
How Python implements the mail function
Why doesn&#39;t Python support function overloading?
Is there function overloading in python
Learning Python third day one-line function
Python function definition and parameter explanation
Python ATM function implementation code example
Python implements AI face change function
Python realizes image recognition car function
Python implements ftp file transfer function
Python realizes udp transmission picture function
How Python implements the timer function
How to run id function in python
What is the id function of python
What is an anonymous function in Python
Comprehensive summary of Python built-in exception types
Centos python3 compile installation and compile gcc upgrade
How does python call its own function
python3 realizes the function of mask drawing
What is the function body of python
Is there a helper function in python