preventing creation of instance

Aahz aahz at
Wed Jun 18 06:05:11 CEST 2003

In article <3EEFBDB3.1D8254F0 at>,
Peter Hansen  <peter at> wrote:
>Alexander Schmolck wrote:
>> Peter Hansen <peter at> writes:
>>> I believe raising any exception in __init__ is the way to prevent
>>> the creation of the object.  I don't think you can do this with
>>> a technique that returns None, unless you are actually calling
>>> a factory function which internally attempts to create the object
>>> and catches the exception thrown, returning None if it happens.
>> >>> class Test(object):
>> ...    def __new__(cls): return None
>> ...
>> >>> Test()
>> >>>
>> see
>D'oh!  I really ought to find a reason to learn about the
>"__new__-fangled" methods in the latest Python releases...  

OTOH, I'm pretty sure Guido would say that Pythonic practice should
stick with raising an exception in such cases.
Aahz (aahz at           <*>

"If you don't know what your program is supposed to do, you'd better not
start writing it."  --Dijkstra

More information about the Python-list mailing list