I'm writing a function in Python to compute some stuff, and if the argument to my function is not of the right type, it fails miserably. So I want to raise an error when that happens.
def do_stuff(x):
if not isinstance(x, int):
raise ValueError(f'Argument {x} is not int')
pass
However, the content of the x
is printed, not the name of x
, which is useful for debugging purposes. In the real case, x
needs to be a pandas.Series
but sometimes is applied to a pandas.DataFrame
. Printing the content of a DataFrame is completely useless for error tracking.
I'm doing something more like this, and tracking the error with the line number. But having the variable name would be awesome
raise ValueError('Argument to do_stuff(x) is not int')
If it's too ugly or convoluted (like looping over all the variables and compare types and values to get a name that match), then I'll give up.
Thanks
P.S.: I'm using Python 3.9, so the f-string syntax is valid. Not sure if there's any other recent version magic that I could use.