Please hear me out. I know how assert
works, I just think it should be used more.
CONTEXT: Some of the applications and usages I have in mind involve mathematical optimization through a GUI, so the functions can be called hundreds of millions of times.
The assert
statement is disabled in optimized mode (-O
) so it is considered bad practice (read: capital sin) to rely on it as per most people's opinion (maybe it's more than an opinion, I don't know).
Say I have the following function that computes the area of a circle:
def area_circle(r):
return 3.141592654 * (r**2)
Yes, I know, there are no docstrings. It's a toy example. To make sure that radius
is either an int
or a float
, I would do this:
def area_circle(r):
assert isinstance(r, (int, float), 'TypeError: Expected int or float, not ' + type(r).__name__
return 3.141592654 * (r**2)
My take on it should probably have me publicly executed, because I see it as a neat feature that allows me to do some input checking and such while I'm developing, but then when I reach a production-ready state, I can just disable those unecessary parts of the code in optimized mode. If my systems tests cover the workflows that lead to the usage of area_circle
, then I personally dont wan't to see raise
statements because they're just waste.
So how can I do an integrated systems tests in a way that doesn't slow down the application without using assert
?
Aucun commentaire:
Enregistrer un commentaire