Reason for globals() in Python?

前端 未结 8 857
北荒
北荒 2020-12-12 12:43

What is the reason of having globals() function in Python? It only returns dictionary of global variables, which are already global, so they can be used anywhere... I\'m ask

8条回答
  •  萌比男神i
    2020-12-12 13:03

    You can pass the result of globals() and locals() to the eval, execfile and __import__ commands. Doing so creates a restricted environment for those commands to work in.

    Thus, these functions exist to support other functions that benefit from being given an environment potentially different from the current context. You could, for example, call globals() then remove or add some variables before calling one of those functions.

提交回复
热议问题