About 562,000 results
Open links in new tab
  1. What is the use of "assert" in Python? - Stack Overflow

    Feb 28, 2011 · Python’s assert statement is a debugging aid, not a mechanism for handling run-time errors. The goal of using assertions is to let developers find the likely root cause of a bug …

  2. python - "assert" statement with or without parentheses - Stack …

    I share your annoyance that the python assert has unique syntax relative to all other python programming constructs, and this syntax has yet again changed from python2 to python3 and …

  3. python - Best practice for using assert? - Stack Overflow

    "assert" statements are removed when the compilation is optimized. So, yes, there are both performance and functional differences. The current code generator emits no code for an …

  4. How can I check if an object has an attribute? - Stack Overflow

    Assert isn't considered safe in production code; it's tempting to write assert x<5 and then be very surprised when x is more than 5 and the software continues running because Python was run …

  5. python - What's the difference between raise, try, and assert?

    Feb 5, 2021 · 108 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 …

  6. Proper way to assert type of variable in Python - Stack Overflow

    Sep 5, 2012 · Note that Unicode strings which only contain ASCII will fail if checking types in this way, so you may want to do something like assert type(s) in (str, unicode) or assert …

  7. assert - How to handle AssertionError in Python and find out …

    Jul 21, 2012 · The traceback module and sys.exc_info are overkill for tracking down the source of an exception. That's all in the default traceback. So instead of calling exit (1) just re-raise: try: …

  8. How do you test that a Python function throws an exception?

    Sep 25, 2008 · On Python < 2.7 this construct is useful for checking for specific values in the expected exception. The unittest function assertRaises only checks if an exception was raised.

  9. How should I verify a log message when testing Python code …

    It works well for me both for Python 2.7 (if you install mock) and for Python 3.4. """ Demo using a mock to test logging output. """ import logging try: import unittest except ImportError: import …

  10. Python: how to determine if an object is iterable?

    When you use for item in o for some iterable object o, Python calls iter(o) and expects an iterator object as the return value. An iterator is any object which implements a __next__ (or next in …