Clean way to not get object back from instantiation attempt gone bad

Georg Brandl g.brandl-nospam at gmx.net
Fri Aug 18 08:35:23 EDT 2006


tobiah wrote:
> Suppose I do:
> 
> 
> myfoo = Foo('grapes', 'oranges')
> 
> And in the __init__() of Foo, there is
> a real problem with the consumption of fruit.
> Is there a clean way to ensure that myfoo
> will be None after the call?  Would the
> __init__() just do del(self), or is there
> a better way to think about this?

There is a way, of course, that results in myfoo
being None in case of an error, but it is not a one-liner and
I'd not recommend it.
If something goes wrong, raising an exception is the best thing to do.

Georg



More information about the Python-list mailing list