Difference between Raise Try and Assert

Defneit picture Defneit · Oct 21, 2016 · Viewed 52.2k times · Source

I have been learning Python for a while and the raise function and assert are (what I realised is that both of them crash the app, unlike try - except) really similar and I can't see a situation where you would use raise or assert over try.

So, what is the difference between Raise, Try and Assert?

Answer

Michal Krzyz picture Michal Krzyz · Oct 21, 2016
assert cond, "text"

is expanded to something like

if cond == False:
  raise AssertionError("text")

use assert because it is more readable.