Avoiding More Pitfalls
In this section of the course, you’ll see some more common pitfalls when using Python’s
assert statement. Another subtle pitfall with the
assert statement appears when you use this statement to check operations, functions, or expressions that have some kind of side effect. In other words, these operations modify the state of objects outside the operation’s scope.
00:23 In those situations, the side effect takes place every time your code runs the assertion, which might silently change your program’s global state and behavior.
00:34 Consider the toy example seen on-screen, in which a function modifies the value of a global variable as a side effect.
In this example,
item at a given
index in the input
sample of data, with the side effect of also removing that
Using assertions to make sure that your function is returning the correct item can seem appropriate. However, this will cause the function’s internal side effect to run in every assertion, modifying the original content of
01:14 To prevent unexpected behaviors like this one, use assertion expressions that don’t cause side effects.
01:23 For example, you can use pure functions that just take input arguments and return the corresponding output without modifying the state of objects from other scopes and namespaces.
01:36 Too many assertions in production can impact your code’s performance. This issue becomes critical when the asserted conditions involve too much logic, such as long compound conditions, long-running predicate functions, and classes implying a costly instantiation process.
Assertions can impact your code’s performance in two main ways. They will take time to execute and use extra memory. An
assert statement that checks for a
None value can be relatively inexpensive.
02:08 However, more complex assertions, especially those running heavy code, can measurably slow down your code. Assertions also consume memory to store their own code and any required data.
To avoid performance issues in production code, you should set Python’s command-line options or the
PYTHONOPTIMIZE environment variable according to your needs.
02:31 Either strategy will optimize your code by generating an assertions-free compiled bytecode, which will run more quickly and take up less memory. Additionally, to prevent performance issues during development, your assertions should be fairly slim and to the point.
In Python, assertions are enabled by default. When the interpreter runs in normal mode, the
__debug__ variable is true, and assertions are enabled.
02:57 This behavior makes sense because you typically develop, debug, and test your code in normal mode. If you want to disable assertions in Python, then it needs to be done explicitly. In contrast, other programming languages have assertions disabled by default. For example, if you’re coming into Python from Java, you may assume that assertions won’t run unless you explicitly turn them on.
03:21 This assumption can be a common source of confusion for Python beginners, so keep it in mind.
03:29 In the next section of the course, you’ll take a look back at what you’ve learned.
Become a Member to join the conversation.