all
I have a question for class instantiation in python.
So I have a bunch of data of different types stored in the same directory and I only want to process one type of them using the python class only good for that type. Unfortunately the type of the data is only known when it is read in through the class.
So I wonder if there is a way to simply stop the class instantiation in __init__() if the data type is not correct and just simply pass to the next dataset when reading all the data in?
Or it is a bad idea to validate upon a class instantiation?
Thanks a lot!
The correct way to do this is to raise an error if the data fed to the class is the wrong type:
And then wrap your instantiation with a try except clause:
This is advantageous because it makes the fact that the data needs to be a certain type explicitly obvious.
Another option is to do as sberry said, and explicitly test the data type before attempting to instantiate a class: