Python __init__ return failure to create

Question:

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)

Asked By: Philip Massey

||

Answers:

Have you considered raising an exception? That is the usual way to signal a failure.

Answered By: D.Shawley

You could raise an exception when either assertio fail, or -, if you really don’t want or can’t work with exceptions, you can write the __new__ method in your classes –
in Python, __init__ is technically an “initializer” method – and it should fill in he attributes and acquire some of the resources and others your object will need during its life cycle – However, Python does define a real constructor, the __new__ method, which is called prior to __init__– and unlike this, __new__ actually does return a value: the newly created (uninitialized) instance itself.

So you can place your checks inside __new__ and and simply return None is something fails – otherwise, return the result of the call to the superclass __new__ method (one can’t do the actual memory allocation for the object in pure Python, so ultimately you have to call a constructor written in native code in a superclass – usually this is object.__new__ in the base of your class hierarchy.

NB: In Python 2, you must have object as the base class for your hierarchy – otherwise not only __new__ is not called, as a whole lot of features added later to Python objects will just not work. In short, class MyClass(object): , never class MyClass: – unless you are on Python3.

Answered By: jsbueno

What about writing a @classmethod function? You can then check all the conditions and either create and initialize a new class, or return None. The calling function then just has to check if the returned class is not None.

Answered By: Andrew Leason

I returned __init__ using some tricks.

This is how to set the error to ‘try’ by making an error in __init__.

Below is an example code.

import sys

class MyClass(object):
    def __init__(self, test=None):
        if test is None:
            sys.exit(1)

        self.test = test
        print("do something __init__ here")

try:
    obj = MyClass()
except:
    obj = None

print(obj)
Answered By: Hong승원
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.