Issue
I am working with a notebook that uses the following syntax to set variables to later raise exceptions in the code:
data_format = raise NotImplementedError("Select a data format for your kaggle entry! See docstring of 'assemble_predictors_predictands' for description of data_format.")
But VS Code raises this error when running the notebook:
data_format = raise NotImplementedError("Select a data format for your kaggle entry! See docstring of 'assemble_predictors_predictands' for description of data_format.")
^ SyntaxError: invalid syntax`
What is the correct way to set a variable equal to an exception?
Solution
"raise" is a statement. Trying to assign a "raise" statement to a variable and later just mention that variable is an indicator you should learn some more of Python syntax, imperative programing and functions
If you want to try going ahead without stepping back and checking these concepts anyway you can write it as a function instead
def data_format():
raise NotImplementedError(...)
And call it like a function.
Just for keeping the answer complete: you can also assign an Exception instance to a variable - but you need to use the "raise" statement at the point you want the exception to take place, not just mention, or try to use it:
data_format = NotImplementedError(...)
...
raise data_format
You really shouldn't do this, though, because if you end up raising the same exception object twice, you'll get messed-up tracebacks. Constructing an exception when you raise it is less error-prone.
Answered By - jsbueno
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.