Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
5 views
in Data Science by (18.4k points)
Is it a bad form to raise exceptions within __init__? If so, then what is an accepted method of throwing an error when certain class variables are initialized as None or of an incorrect type?

1 Answer

0 votes
by (36.8k points)

Raising exceptions within __init__() is absolutely fine. There's no other good way to indicate the error condition within a constructor, and there are many hundreds of examples in the standard library where building an object can raise an exception.

The error class is to raise. It is up to you. ValueError is best if the constructor was passed an invalid parameter.

If you want to know more about the Data Science then do check out the following Data Science which will help you in understanding Data Science from scratch

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...