Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What is the correct way to assign an exception to a variable in a Python notebook?

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 Not implemented error(...)

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading