preventing creation of instance

Peter Hansen peter at engcorp.com
Tue Jun 17 21:17:39 EDT 2003


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...  

I just can't seem to find a reason to use them though (yet).

-Peter




More information about the Python-list mailing list