English documents:
-
-
exec
(
object[,
globals[,
locals]])
-
-
-
This function supports dynamic execution of Python Code.
object must be either a string or a code Object. If It is a string, the string is parsed as a suite of Python statements which are then executed (unless a syntax error OCCU Rs). [1] If It is a code object, it is simply Executed. In all cases, the code That's executed is expected to being valid as file input (see the section "file input" in the Referenc E Manual). Be aware that the and statements May is
return
yield
used outside of function definitions even within the context O F code passed to the
exec()
Function. The return value is
None
.
-
-
-
In all cases, if the optional parts was omitted, the code is executed in the current Scope. If only
globals are provided, it must be a dictionary, which would be used for both the global and the local Vari Ables. If
Globals and
locals are given, they is used for the global and local variables, respectively. If provided,
Locals can is any mapping object. Remember that at module level, Globals and locals is the same dictionary. If exec gets both separate objects as
globals and
locals, the code would be executed as if it were EMB Edded in a class Definition.
-
-
-
If the
Globals dictionary does not contain a value
__builtins__
for the key, a reference to the dictionary of the BU Ilt-in module is
builtins
inserted under this key. That's the I can control what builtins is available to the executed code by inserting your own
__builtins__
dictionary into
Globals before passing it to
exec()
.
-
-
-
Note
-
-
-
The built-in functions
globals()
and
locals()
return the current global and local dictionary, respectively, which could be USEF UL to pass around-the second and third argument to
exec()
.
-
-
-
Note
-
-
-
The default
locals act as described for function
locals()
below:modifications to the default
locals Dictionary should not being Attempted. Pass an explicit
locals dictionary If you need to see effects of the the code on
locals after function
exec()
Returns.
-
-
-
-
Description
-
-
-
-
-
1. The EXEC function is similar to the Eval function and executes dynamic statements, except that the Eval function is only used to perform expression evaluation, and the EXEC function is primarily used to execute statement blocks.
-
>>> Eval ('a=1+2')#EXECUTE statement ErrorTraceback (most recent): File"<pyshell#0>", Line 1,inch<module>Eval ('a=1+2') File"<string>", Line 1a=1+2 ^syntaxerror:invalid Syntax>>>exec('a=1+2')#EXECUTE Statement>>>a3
2. The first argument is a statement string, the globals parameter and the locals parameter are optional parameters, and if provided, the globals parameter must be a dictionary, and the locals parameter is the mapping Object.
3. The globals parameter is used to specify the global variables that can be used when code executes and to collect the global variables after code execution
>>> g = {'Num': 2}>>>type (g)<class 'Dict'>>>>exec('num2 = num + 2', G)>>> g['Num']2>>> g['num2']#num2 global variables defined in exec are collected4
4. The locals parameter is used to specify local variables that can be used when code executes and to collect local variables after code execution
>>> g = {'Num': 2}>>>type (g)<class 'Dict'>>>> L = {'num2': 3}>>>type (l)<class 'Dict'>>>>exec(" "num2 = 13num3 = num + num2" ", G,l)>>> l['num2']#l The value of num2 has changed13
5. To ensure that the code runs successfully, the globals parameter dictionary does not contain the key __builtins__ , and Python automatically adds a key of __builtins__ . Value is a reference to the Builtins Module. If you do want to restrict the code from using the Builtins module, you need to add a key of __builtins__,value {} to global (very few people do that).
>>> g = {}>>>exec('A = ABS ( -1)', G)>>> >>> g = {'__builtins__':{}}>>>exec('A = ABS ( -1)', G)#you can't use built-in Functions.Traceback (most recent): File"<pyshell#30>", Line 1,inch<module>exec('A = ABS ( -1)', G) File"<string>", Line 1,inch<module>Nameerror:name'ABS' is notDefined
6. When the globals parameter is not provided yes, python defaults to use the dictionary returned by the Globals () function to Invoke. When the locals parameter is not provided, it is called by default using the Globals Parameter.
>>> num = 1>>>exec('num2 = num + 1')>>>Globals () {'__package__': None,'__loader__': <class '_frozen_importlib. Builtinimporter','__name__':'__main__','__spec__': None,'__builtins__': <module'Builtins'(built-inch);'__doc__': None,'num2': 2,'Num': 1}>>> >>> >>>exec('num2 = num + 1',{})#globals parameter specified, no num variable execution failed in GlobalsTraceback (most recent): File"<pyshell#5>", Line 1,inch<module>exec('num2 = num + 1',{}) File"<string>", Line 1,inch<module>Nameerror:name'Num' is notdefined>>> L =Locals ()>>>l{'__package__': None,'__loader__': <class '_frozen_importlib. Builtinimporter','__name__':'__main__','__spec__': None,'__builtins__': <module'Builtins'(built-inch);'__doc__': None,'L': {...},'num2': 2,'Num': 1}>>> >>>exec('num3 = num + 1', {},l)#globals parameter specified, no num variable in globals, locals variable specified, locals variable containing num variable executed successfully>>>l{'__package__': None,'__loader__': <class '_frozen_importlib. Builtinimporter','__name__':'__main__','__spec__': None,'num3': 2,'__builtins__': <module'Builtins'(built-inch);'__doc__': None,'L': {...},'num2': 2,'Num': 1}>>>
Python built-in function (--exec)