Friday, June 28, 2019

Python assert Statement

In this tutorial you’ll learn about assert statement in Python which is used to assert the trueness of a condition.

Assertion is a boolean expressions to check if the condition is true or false. In case it is true, the program does nothing and move to the next line of code. If the condition is false, the program stops and throws an error.

Python assert statement

In Python there is an assert statement to assert if a given condition is true or not. If the condition is false execution stops and AssertionError is raised.

Syntax of assert statement in Python-

assert expression, message

Here message is optional, if no message is passed then the program stops and raises AssertionError if the condition is false.

If message is passed along with the assert statement then the program stops and raises AssertionError + display error message, if the condition is false.

Assert statement example

1- Using assert statement without any message.

def divide(num1, num2):
    assert num2 != 0
    result = num1/num2
    print('Result-', result)

divide(16, 0)

Output

Traceback (most recent call last):
  File "F:/NETJS/NetJS_2017/Python/Test/Test.py", line 6, in <module>
    divide(16, 0)
  File "F:/NETJS/NetJS_2017/Python/Test/Test.py", line 2, in divide
    assert num2 != 0
AssertionError

In the example there is an assert statement with a condition that the second argument should not be zero. Since the assert condition returns false AssertionError is raised.

2- When the condition in assert statement is true.
def divide(num1, num2):
    assert num2 != 0
    result = num1/num2
    print('Result-', result)

divide(16, 2)

Output

Result- 8.0

3- Using assert statement with a message. If an optional message is passed then the message is also displayed if the condition is false.

def divide(num1, num2):
    assert num2 != 0, "Second argument can't be passed as zero"
    result = num1/num2
    print('Result-', result)

divide(16, 0)

Output

Traceback (most recent call last):
  File "F:/NETJS/NetJS_2017/Python/Test/Test.py", line 6, in <module>
    divide(16, 0)
  File "F:/NETJS/NetJS_2017/Python/Test/Test.py", line 2, in divide
    assert num2 != 0, "Second argument can't be passed as zero"
AssertionError: Second argument can't be passed as zero

Using assert statement in Python

Assert statement is useful for debugging or quick sanity testing. It should not be used for data validation in the code.

By using PYTHONOPTIMIZE environment variable or by using -O or -OO assert statements can be disabled. In that case assert statements won’t be executed and using them for any critical data validation in your code may cause serious issues.

In Python internally any assert statement is equivalent to-

if __debug__:
    if not expression: raise AssertionError

The built-in variable __debug__ is False when optimization is requested (command line option -O) which means no assert statements are executed.

That's all for this topic Python assert Statement. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Python Tutorial Page


Related Topics

  1. Python continue Statement With Examples
  2. Python break Statement With Examples
  3. Operator Overloading in Python
  4. Global Keyword in Python With Examples
  5. Name Mangling in Python

You may also like-

  1. String Length in Python - len() Function
  2. Magic Methods in Python With Examples
  3. Python Program to Display Fibonacci Series
  4. Nested Try Statements in Java Exception Handling
  5. Race Condition in Java Multi-Threading
  6. Printing Numbers in Sequence Using Threads - Java Program
  7. How to Read Properties File in Spring Framework
  8. What is Hadoop Distributed File System (HDFS)

No comments:

Post a Comment