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

  1. The Python for loop
  2. The Python string
  3. The Python list
  4. The Python tuple tuple
  5. Python dictionary dict
  6. Python conditional derivations
  7. Python list derivations
  8. Python dictionary derivations
  9. Python function declarations and calls
  10. Python variable argument *argc/**kargcs
  11. Python anonymous function lambda
  12. Python return logic determines expressions
  13. Python string/list/tuple/dictionary conversions
  14. Python local and global variables
  15. The Python type function is different from the isinstance function
  16. Python is differs from ==
  17. Python mutable and immutable data types
  18. Shallow and deep copies of Python

Python eval and exec functions

This article is published by the blog – Ape Say Programming Ape Say programming!