Python: Is this a bad form for throwing exceptions during __init__?

Is a bad form causing an exception during __init__ ? If so, what is the acceptable method of throwing an error when certain class variables are initialized to " None or" the wrong type "?

+94
python exception
01 Oct '09 at 23:47
source share
7 answers

Throwing exceptions within __init__() absolutely normal. There is no other good way to specify an error condition inside the constructor, and there are many hundreds of examples in the standard library where creating an object can throw an exception.

The class of error to be raised is, of course, up to you. ValueError best if the constructor has been assigned an invalid parameter.

+124
01 Oct '09 at 23:59
source share

It is true that the only correct way to indicate an error in the constructor is to throw an exception. That is why in C ++ and in other object-oriented languages ​​that were developed taking into account the safety of exceptions, the destructor is not called if the exception is selected in the constructor of the object (which means that the initialization of the object is incomplete). This is often not the case in scripting languages ​​such as Python. For example, the following code throws an AttributeError if socket.connect () fails:

 class NetworkInterface: def __init__(self, address) self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.socket.connect(address) self.stream = self.socket.makefile() def __del__(self) self.stream.close() self.socket.close() 

The reason is that the incomplete object destructor is called after the connection attempt failed before the thread attribute was initialized. You should not avoid throwing exceptions from constructors, I am just saying that it is difficult to write completely safe exception code in Python. Some Python developers generally avoid using destructors, but this is a matter of other debate.

+20
Sep 21 2018-11-21T00:
source share

I see no reason for this to be in bad shape.

In contrast, one of the exceptions for succeeding, unlike returning error codes, is that designers can return error codes, usually they cannot . Thus, at least in languages ​​like C ++, an exception for an exception is the only way to signal errors.

+11
Oct 02 '09 at 0:01
source share

The standard library says:

 >>> f = file("notexisting.txt") Traceback (most recent call last): File "<stdin>", line 1, in <module> IOError: [Errno 2] No such file or directory: 'notexisting.txt' 

Also, I see no reason why it should be considered bad.

+5
01 Oct '09 at 23:58
source share

I think this is the ideal case for a built-in ValueError exception.

+3
Oct 01 '09 at 23:55
source share

I agree with all of the above.

In fact, there is no other way to notify that something went wrong in initializing an object than throwing an exception.

In most program classes, where the state of a class depends entirely on the input of this class, we can expect some type of ValueError or TypeError to be raised.

Classes with side effects (for example, with a network or graphics) can cause an init error if (for example) the network device is not available or the canvas cannot be written. This sounds reasonable to me because often you want to find out about the conditions of failure as soon as possible.

+2
Oct 02 '09 at 0:46
source share

Getting errors from init is inevitable in some cases, but too much work in init is a bad style. You should consider creating a factory or a pseudo-factory, a simple class method that returns an installed object.

+2
Sep 16 '13 at 10:18
source share



All Articles