assert

Proper way to assert type of variable in Python

Proper way to assert type of variable in Python Question: In using a function, I wish to ensure that the type of the variables are as expected. How to do it right? Here is an example fake function trying to do just this before going on with its role: def my_print(begin, text, end): “””Print ‘text’ …

Total answers: 4

What are acceptable use-cases for python's `assert` statement?

What are acceptable use-cases for python's `assert` statement? Question: I often use python’s assert statement to check user input and fail-fast if we’re in a corrupt state. I’m aware that assert gets removed when python with the -o(optimized) flag. I personally don’t run any of my apps in optimized mode, but it feels like I …

Total answers: 8

python optimized mode

What is the use of the "-O" flag for running Python? Question: Python can run scripts in optimized mode (python -O) which turns off debugs, removes assert statements, and IIRC it also removes docstrings. However, I have not seen it used. Is python -O actually used? If so, what for? Asked By: zaharpopov || Source …

Total answers: 4

How to check if an object is a list or tuple (but not string)?

How to check if an object is a list or tuple (but not string)? Question: This is what I normally do in order to ascertain that the input is a list/tuple – but not a str. Because many times I stumbled upon bugs where a function passes a str object by mistake, and the target …

Total answers: 20

What is the use of Python's basic optimizations mode? (python -O)

What is the use of Python's basic optimizations mode? (python -O) Question: Python has a flag -O that you can execute the interpreter with. The option will generate “optimized” bytecode (written to .pyo files), and given twice, it will discard docstrings. From Python’s man page: -O Turn on basic optimizations. This changes the filename extension …

Total answers: 7

Making Python's `assert` throw an exception that I choose

Making Python's `assert` throw an exception that I choose Question: Can I make assert throw an exception that I choose instead of AssertionError? UPDATE: I’ll explain my motivation: Up to now, I’ve had assertion-style tests that raised my own exceptions; For example, when you created a Node object with certain arguments, it would check if …

Total answers: 8

Disable assertions in Python

Disable assertions in Python Question: How do I disable assertions in Python? That is, if an assertion fails, I don’t want it to throw an AssertionError, but to keep going. How do I do that? Asked By: Claudiu || Source Answers: Running in optimized mode should do it: python -OO module.py Answered By: FogleBird Use …

Total answers: 6

Python : Assert that variable is instance method?

Python : Assert that variable is instance method? Question: How can one check if a variable is an instance method or not? I’m using python 2.5. Something like this: class Test: def method(self): pass assert is_instance_method(Test().method) Asked By: quano || Source Answers: If you want to know if it is precisely an instance method use …

Total answers: 2

Best practice for using assert?

Best practice for using assert? Question: Is there a performance or code maintenance issue with using assert as part of the standard code instead of using it just for debugging purposes? Is assert x >= 0, ‘x is less than zero’ better or worse than if x < 0: raise Exception(‘x is less than zero’) …

Total answers: 15