directory
- A preface.
- Introduction to Python eval and exec functions
- Python eval and exec functions
- 1. The exec function executes the code snippet
- 2. the exec function executes the py file
- Four. Guess you like it
Recommended path for learning Python: Python Learning Directory >> Python Basics
A preface.
Python execfile and exec have the same functionality, but execfile is Python version 2.0 and exec is Python version 3.0. Refer to the following details for specific usage methods.
Introduction to Python eval and exec functions
- Exec, the built-in function, can dynamically execute complex code snippets;
- Exec, the built-in function, can execute Python code in the py file;
Exec (Python2.0); exec (Python3.0); exec (Python3.0); exec (Python3.0); exec (Python3.0); exec (Python3.0);
Source-py File content or code segment; Globals - Default arguments, null by default, variable scope, global namespace, if provided, must be a dictionary object; Locals - default argument, null by default, variable scope, local namespace, if provided, can be any mapping object; Return value: The return value is always None; ''' exec(source, globals=None, locals=None)Copy the code
Python eval and exec functions
1. The exec function executes the code snippet
#! Usr /bin/env python # -* -coding :utf-8 _*- "" www.codersrc.com @file :Python eval and exec functions. py @time :2021/04/29 07:37 @Motto: A thousand miles cannot be built without a small step, and a river cannot be built without a small stream. Print (x,y,z,sum) "" def main(): y = 20 a = exec(source_code) # 100+20+30 b = exec(source_code,{'x':10,'y':20}) # 10+20+30 c = The exec (source_code, {' x ': 10,' y ': 20}, {" y ": 3,' z ': 4}) # 10 + 3 + 30, x is to define the global variables 1, Print (a,b,c) # exec return None if __name__ == "__main__": 100 20 30 150 10 20 30 60 10 3 30 43 None None None"Copy the code
Code analysis: Source_code is a complex snippet of code, and exec executes dynamically, much more powerful than eval.
2. the exec function executes the py file
The exec function, in addition to executing complex code snippets, can also execute Python code in py files. For example:
#! Usr /bin/env python # -* -coding :utf-8 _*- "" www.codersrc.com @file :Python eval and exec functions. py @time :2021/04/29 07:37 @Motto: A thousand miles cannot be built without a small step, and a river cannot be built without a small stream. """ # e:/test.txt def main(): x = 20 y = 50 print(x+y) print("www.codersrc.com") if __name__ == "__main__": main()Copy the code
Then use the built-in exec function to execute the Python code for this TXT file:
With open('e://test.txt','r') as f: exec(f.read()) "70 www.codersrc.com"Copy the code
Four.Guess you like
- The Python for loop
- The Python string
- The Python list
- The Python tuple tuple
- Python dictionary dict
- Python conditional derivations
- Python list derivations
- Python dictionary derivations
- Python function declarations and calls
- Python variable argument *argc/**kargcs
- Python anonymous function lambda
- Python return logic determines expressions
- Python string/list/tuple/dictionary conversions
- Python local and global variables
- The Python type function is different from the isinstance function
- Python is differs from ==
- Python mutable and immutable data types
- Shallow and deep copies of Python
Python eval and exec functions
This article is published by the blog – Ape Say Programming Ape Say programming!