preventing creation of instance

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


In article <3EEFBDB3.1D8254F0 at engcorp.com>,
Peter Hansen  <peter at engcorp.com> wrote:
>Alexander Schmolck wrote:
>> Peter Hansen <peter at engcorp.com> 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 http://www.python.org/2.2/descrintro.html#__new__
>
>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 pythoncraft.com)           <*>         http://www.pythoncraft.com/

"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