2

I have a python program that needs to assert a lot of nested conditions before executing.

Python has a way to assert with assert statement,

Syntex: assert condition, error_message(optional)

but, I need to assert multiple nested conditions, and probably execute multiple statements in the assert.

What is proper way to use assert in python?


My thinking is: check c only when b > a is true and check a > c only both are true. And after assertion executes multiple statements like:- log the info and print the info etc..

Pseudocode:

if c != null then
  if b > a then
    if a > c then
     print 'yippee!'
    else
     throw exception('a > c error')
  else
    throw exception('b > a error')
else
  throw exception('c is null')
3
  • 3
    Note that assert is only for basic sanity checking and debug purposes. It should not form an integral part of your business logic! Commented Mar 26, 2020 at 6:01
  • 1
    so how do I do sanity check my program. with normal IF ELSE? Commented Mar 26, 2020 at 6:06
  • For implementing vital business logic checks, yes, good old if..else or similar tools. Commented Mar 26, 2020 at 6:07

1 Answer 1

2

Do you mean something like this?

a = 1
b = 2
c = 3

assert b > a and c > b and a > c, 'Error: a is not greater than c'

Output:

Traceback (most recent call last):
  File "main.py", line 6, in <module>
    assert b > a and c > b and a > c, 'Error: a is not greater than c'
AssertionError: Error: a is not greater than c

something like this:- check c only when b > a is true and check a > c only both are true. And after assertion executes multiple statements like:- log the info and print the info etc..

You can use several assert statements one after another, like you'ld write several if statements under each other, except you've to take into account that your asserts can raise an exception, which you need to take care of. This way you can simply control the execution flow, and print/log whatever you need... For example something like this:

def make_comperations(a, b, c = None): # note c is optional
  assert c is not None,  'Error: c is None'
  assert b > a, 'Error: a is not greater than b'
  assert c > a, 'Error: c is not greater than a'


try:
  make_comperations(1, 2, 3) # ok
  make_comperations(1, 2) # error
  make_comperations(1, 2, 0) # error, but won't be executed since the line above will throw an exception
  print("All good!")
except AssertionError as err:
  if str(err) == 'Error: c is None':
    print("Log: c is not given!")
  if str(err) == 'Error: a is not greater than b':
    print("Log: b > a error!")
  elif str(err) == 'Error: c is not greater than a':
    print("Log: c > a error!")

Output:

Log: c is not given!
Sign up to request clarification or add additional context in comments.

1 Comment

something like this:- check c only when b > a is true and check a > c only both are true. And after assertion executes multiple statements like:- log the info and print the info etc..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.