Linked to, but not explicitly mentioned here, is exactly when __all__
is used. It is a list of strings defining what symbols in a module will be exported when from <module> import *
is used on the module.
For example, the following code in a foo.py
explicitly exports the symbols bar
and baz
:
__all__ = ['bar', 'baz']
waz = 5
bar = 10
def baz(): return 'baz'
These symbols can then be imported like so:
from foo import *
print(bar)
print(baz)
# The following will trigger an exception, as "waz" is not exported by the module
print(waz)
If the __all__
above is commented out, this code will then execute to completion, as the default behaviour of import *
is to import all symbols that do not begin with an underscore, from the given namespace.
Reference: https://docs.python.org/tutorial/modules.html#importing-from-a-package
NOTE: __all__
affects the from <module> import *
behavior only. Members that are not mentioned in __all__
are still accessible from outside the module and can be imported with from <module> import <member>
.
Maybe a bit of example code will help: Notice the difference in the call signatures of foo
, class_foo
and static_foo
:
class A(object):
def foo(self, x):
print(f"executing foo({self}, {x})")
@classmethod
def class_foo(cls, x):
print(f"executing class_foo({cls}, {x})")
@staticmethod
def static_foo(x):
print(f"executing static_foo({x})")
a = A()
Below is the usual way an object instance calls a method. The object instance, a
, is implicitly passed as the first argument.
a.foo(1)
# executing foo(<__main__.A object at 0xb7dbef0c>, 1)
With classmethods, the class of the object instance is implicitly passed as the first argument instead of self
.
a.class_foo(1)
# executing class_foo(<class '__main__.A'>, 1)
You can also call class_foo
using the class. In fact, if you define something to be
a classmethod, it is probably because you intend to call it from the class rather than from a class instance. A.foo(1)
would have raised a TypeError, but A.class_foo(1)
works just fine:
A.class_foo(1)
# executing class_foo(<class '__main__.A'>, 1)
One use people have found for class methods is to create inheritable alternative constructors.
With staticmethods, neither self
(the object instance) nor cls
(the class) is implicitly passed as the first argument. They behave like plain functions except that you can call them from an instance or the class:
a.static_foo(1)
# executing static_foo(1)
A.static_foo('hi')
# executing static_foo(hi)
Staticmethods are used to group functions which have some logical connection with a class to the class.
foo
is just a function, but when you call a.foo
you don't just get the function,
you get a "partially applied" version of the function with the object instance a
bound as the first argument to the function. foo
expects 2 arguments, while a.foo
only expects 1 argument.
a
is bound to foo
. That is what is meant by the term "bound" below:
print(a.foo)
# <bound method A.foo of <__main__.A object at 0xb7d52f0c>>
With a.class_foo
, a
is not bound to class_foo
, rather the class A
is bound to class_foo
.
print(a.class_foo)
# <bound method type.class_foo of <class '__main__.A'>>
Here, with a staticmethod, even though it is a method, a.static_foo
just returns
a good 'ole function with no arguments bound. static_foo
expects 1 argument, and
a.static_foo
expects 1 argument too.
print(a.static_foo)
# <function static_foo at 0xb7d479cc>
And of course the same thing happens when you call static_foo
with the class A
instead.
print(A.static_foo)
# <function static_foo at 0xb7d479cc>
Best Answer
The short answer, or TL;DR
Basically,
eval
is used to evaluate a single dynamically generated Python expression, andexec
is used to execute dynamically generated Python code only for its side effects.eval
andexec
have these two differences:eval
accepts only a single expression,exec
can take a code block that has Python statements: loops,try: except:
,class
and function/methoddef
initions and so on.An expression in Python is whatever you can have as the value in a variable assignment:
eval
returns the value of the given expression, whereasexec
ignores the return value from its code, and always returnsNone
(in Python 2 it is a statement and cannot be used as an expression, so it really does not return anything).In versions 1.0 - 2.7,
exec
was a statement, because CPython needed to produce a different kind of code object for functions that usedexec
for its side effects inside the function.In Python 3,
exec
is a function; its use has no effect on the compiled bytecode of the function where it is used.Thus basically:
The
compile
in'exec'
mode compiles any number of statements into a bytecode that implicitly always returnsNone
, whereas in'eval'
mode it compiles a single expression into bytecode that returns the value of that expression.In the
'eval'
mode (and thus with theeval
function if a string is passed in), thecompile
raises an exception if the source code contains statements or anything else beyond a single expression:Actually the statement "eval accepts only a single expression" applies only when a string (which contains Python source code) is passed to
eval
. Then it is internally compiled to bytecode usingcompile(source, '<string>', 'eval')
This is where the difference really comes from.If a
code
object (which contains Python bytecode) is passed toexec
oreval
, they behave identically, excepting for the fact thatexec
ignores the return value, still returningNone
always. So it is possible useeval
to execute something that has statements, if you justcompile
d it into bytecode before instead of passing it as a string:works without problems, even though the compiled code contains statements. It still returns
None
, because that is the return value of the code object returned fromcompile
.In the
'eval'
mode (and thus with theeval
function if a string is passed in), thecompile
raises an exception if the source code contains statements or anything else beyond a single expression:The longer answer, a.k.a the gory details
exec
andeval
The
exec
function (which was a statement in Python 2) is used for executing a dynamically created statement or program:The
eval
function does the same for a single expression, and returns the value of the expression:exec
andeval
both accept the program/expression to be run either as astr
,unicode
orbytes
object containing source code, or as acode
object which contains Python bytecode.If a
str
/unicode
/bytes
containing source code was passed toexec
, it behaves equivalently to:and
eval
similarly behaves equivalent to:Since all expressions can be used as statements in Python (these are called the
Expr
nodes in the Python abstract grammar; the opposite is not true), you can always useexec
if you do not need the return value. That is to say, you can use eithereval('my_func(42)')
orexec('my_func(42)')
, the difference being thateval
returns the value returned bymy_func
, andexec
discards it:Of the 2, only
exec
accepts source code that contains statements, likedef
,for
,while
,import
, orclass
, the assignment statement (a.k.aa = 42
), or entire programs:Both
exec
andeval
accept 2 additional positional arguments -globals
andlocals
- which are the global and local variable scopes that the code sees. These default to theglobals()
andlocals()
within the scope that calledexec
oreval
, but any dictionary can be used forglobals
and anymapping
forlocals
(includingdict
of course). These can be used not only to restrict/modify the variables that the code sees, but are often also used for capturing the variables that theexec
uted code creates:(If you display the value of the entire
g
, it would be much longer, becauseexec
andeval
add the built-ins module as__builtins__
to the globals automatically if it is missing).In Python 2, the official syntax for the
exec
statement is actuallyexec code in globals, locals
, as inHowever the alternate syntax
exec(code, globals, locals)
has always been accepted too (see below).compile
The
compile(source, filename, mode, flags=0, dont_inherit=False, optimize=-1)
built-in can be used to speed up repeated invocations of the same code withexec
oreval
by compiling the source into acode
object beforehand. Themode
parameter controls the kind of code fragment thecompile
function accepts and the kind of bytecode it produces. The choices are'eval'
,'exec'
and'single'
:'eval'
mode expects a single expression, and will produce bytecode that when run will return the value of that expression:'exec'
accepts any kinds of python constructs from single expressions to whole modules of code, and executes them as if they were module top-level statements. The code object returnsNone
:'single'
is a limited form of'exec'
which accepts a source code containing a single statement (or multiple statements separated by;
) if the last statement is an expression statement, the resulting bytecode also prints therepr
of the value of that expression to the standard output(!).An
if
-elif
-else
chain, a loop withelse
, andtry
with itsexcept
,else
andfinally
blocks is considered a single statement.A source fragment containing 2 top-level statements is an error for the
'single'
, except in Python 2 there is a bug that sometimes allows multiple toplevel statements in the code; only the first is compiled; the rest are ignored:In Python 2.7.8:
And in Python 3.4.2:
This is very useful for making interactive Python shells. However, the value of the expression is not returned, even if you
eval
the resulting code.Thus greatest distinction of
exec
andeval
actually comes from thecompile
function and its modes.In addition to compiling source code to bytecode,
compile
supports compiling abstract syntax trees (parse trees of Python code) intocode
objects; and source code into abstract syntax trees (theast.parse
is written in Python and just callscompile(source, filename, mode, PyCF_ONLY_AST)
); these are used for example for modifying source code on the fly, and also for dynamic code creation, as it is often easier to handle the code as a tree of nodes instead of lines of text in complex cases.While
eval
only allows you to evaluate a string that contains a single expression, you caneval
a whole statement, or even a whole module that has beencompile
d into bytecode; that is, with Python 2,print
is a statement, and cannot beeval
led directly:compile
it with'exec'
mode into acode
object and you caneval
it; theeval
function will returnNone
.If one looks into
eval
andexec
source code in CPython 3, this is very evident; they both callPyEval_EvalCode
with same arguments, the only difference being thatexec
explicitly returnsNone
.Syntax differences of
exec
between Python 2 and Python 3One of the major differences in Python 2 is that
exec
is a statement andeval
is a built-in function (both are built-in functions in Python 3). It is a well-known fact that the official syntax ofexec
in Python 2 isexec code [in globals[, locals]]
.Unlike majority of the Python 2-to-3 porting guides seem to suggest, the
exec
statement in CPython 2 can be also used with syntax that looks exactly like theexec
function invocation in Python 3. The reason is that Python 0.9.9 had theexec(code, globals, locals)
built-in function! And that built-in function was replaced withexec
statement somewhere before Python 1.0 release.Since it was desirable to not break backwards compatibility with Python 0.9.9, Guido van Rossum added a compatibility hack in 1993: if the
code
was a tuple of length 2 or 3, andglobals
andlocals
were not passed into theexec
statement otherwise, thecode
would be interpreted as if the 2nd and 3rd element of the tuple were theglobals
andlocals
respectively. The compatibility hack was not mentioned even in Python 1.4 documentation (the earliest available version online); and thus was not known to many writers of the porting guides and tools, until it was documented again in November 2012:Yes, in CPython 2.7 that it is handily referred to as being a forward-compatibility option (why confuse people over that there is a backward compatibility option at all), when it actually had been there for backward-compatibility for two decades.
Thus while
exec
is a statement in Python 1 and Python 2, and a built-in function in Python 3 and Python 0.9.9,has had identical behaviour in possibly every widely released Python version ever; and works in Jython 2.5.2, PyPy 2.3.1 (Python 2.7.6) and IronPython 2.6.1 too (kudos to them following the undocumented behaviour of CPython closely).
What you cannot do in Pythons 1.0 - 2.7 with its compatibility hack, is to store the return value of
exec
into a variable:(which wouldn't be useful in Python 3 either, as
exec
always returnsNone
), or pass a reference toexec
:Which a pattern that someone might actually have used, though unlikely;
Or use it in a list comprehension:
which is abuse of list comprehensions (use a
for
loop instead!).