Example use of assert in Python?

后端 未结 4 2100
庸人自扰
庸人自扰 2021-02-04 12:06

I\'ve read about when to use assert vs. exceptions, but I\'m still not \"getting it\". It seems like whenever I think I\'m in a situation where I should use assert, later on in

4条回答
  •  后悔当初
    2021-02-04 13:01

    A good guideline is using assert when its triggering means a bug in your code. When your code assumes something and acts upon the assumption, it's recommended to protect this assumption with an assert. This assert failing means your assumption isn't correct, which means your code isn't correct.

提交回复
热议问题