The text below is selected, press Ctrl+C to copy to your clipboard. (⌘+C on Mac) No line numbers will be copied.
Guest
Python variable name resolution eval() exec() functions, python 3.11.0 docs
By Guest on 4th November 2022 07:27:36 AM | Syntax: PYTHON | Views: 185



New Paste New paste | Download Paste Download | Toggle Line Numbers Show/Hide line no. | Copy Paste Copy text to clipboard
  1. 4.2. Naming and binding
  2. =======================
  3.  
  4. 4.2.4. Interaction with dynamic features
  5. ----------------------------------------
  6.  
  7. Name resolution of free variables occurs at runtime, not at compile
  8. time. This means that the following code will print 42:
  9.  
  10.    i = 10
  11.    def f():
  12.        print(i)
  13.    i = 42
  14.    f()
  15.  
  16. The "eval()" and "exec()" functions do not have access to the full
  17. environment for resolving names.  Names may be resolved in the local
  18. and global namespaces of the caller.  Free variables are not resolved
  19. in the nearest enclosing namespace, but in the global namespace.  [1]
  20. The "exec()" and "eval()" functions have optional arguments to
  21. override the global and local namespace.  If only one namespace is
  22. specified, it is used for both.
  23.  
  24. Related questions:
  25. What is eval and exec in Python?
  26. What is exec () in Python?
  27. What does eval () do in Python?
  28. How do you use eval?
  29.  
  30.  
  31. Related video:
  32. Be Careful When Using exec() or eval() in Python
  33. https://www.youtube.com/watch?v=keSvLnLNep4
















Python software and documentation are licensed under the PSF License Agreement.
Starting with Python 3.8.6, examples, recipes, and other code in the documentation are dual licensed under the PSF License Agreement and the Zero-Clause BSD license.
Some software incorporated into Python is under different licenses. The licenses are listed with code falling under that license. See Licenses and Acknowledgements for Incorporated Software for an incomplete list of these licenses.

Python and it's documentation is:
Copyright © 2001-2022 Python Software Foundation. All rights reserved.
Copyright © 2000 BeOpen.com. All rights reserved.
Copyright © 1995-2000 Corporation for National Research Initiatives. All rights reserved.
Copyright © 1991-1995 Stichting Mathematisch Centrum. All rights reserved.

See History and License for complete license and permissions information:
https://docs.python.org/3/license.html#psf-license
  • Recent Pastes