exec statement is a function in Python 3

suggest change

In Python 2, exec is a statement, with special syntax: exec code [in globals[, locals]]. In Python 3 exec is now a function: exec(code, [, globals[, locals]]), and the Python 2 syntax will raise a SyntaxError.

As print was changed from statement into a function, a __future__ import was also added. However, there is no from __future__ import exec_function, as it is not needed: the exec statement in Python 2 can be also used with syntax that looks exactly like the exec function invocation in Python 3. Thus you can change the statements

exec 'code'
exec 'code' in global_vars
exec 'code' in global_vars, local_vars

to forms

exec('code')
exec('code', global_vars)
exec('code', global_vars, local_vars)

and the latter forms are guaranteed to work identically in both Python 2 and Python 3.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Incompatibilities moving from Python 2 to Python 3:
* map
* exec statement is a function in Python 3

Table Of Contents
2 Filter
3 List
7 Loops
22 Reduce
27 Classes
31 Set
39 Incompatibilities moving from Python 2 to Python 3
42 Tuple
45 Enum
62 Sockets
89 urllib
92 Idioms
104 Stack
105 Profiling
109 Logging
111 os module
118 Mixins
120 ArcPy
126 Arrays
132 2to3 tool
135 Unicode
138 Neo4j
140 Curses
141 Templates
145 heapq
146 tkinter
154 Audio
155 pyglet
157 ijson
160 Flask
161 Groupby
163 pygame
165 hashlib
166 Gzip
167 ctypes
185 pyaudio
186 shelve