First off, I know that the __init__()
function of a class in Python cannot return a value, so sadly this option is unavailable.
Due to the structure of my code, it makes sense to have data assertions (and prompts for the user to give information) inside the __init__
function of the class. However, this means that the creation of the object can fail, and I would like to be able to gracefully recover from this.
I was wondering what the best way to continue with this is. I've considered setting a global boolean as a 'valid construction' flag, but I'd prefer not to.
Any other ideas (besides restructuring so assertions can happen outside of the initialization and values are passed in as arguments)? I'm basically looking for a way to have return 0 on success and return -1 on failure during initialization. (Like most C system calls)