Twice instanciation

user at domain.invalid user at domain.invalid
Fri Jun 13 08:49:54 EDT 2003


Michele Simionato wrote:


> 
> You can do this:
> 
> class DoubleInstantiationError(Exception): pass
> 
> class Global:
>     alreadyinstantiated=False
>     def __init__(self,init=0):
>         if self.__class__.alreadyinstantiated:
>             raise DoubleInstantiationError()
>         self.value = init
>         self.__class__.alreadyinstantiated=True
> 
> g = Global() #instanciating one time ok
> print g
> g = Global() #instanciating two times would raise an exception


Until there it's ok ;-)


> Notice that you would have an exception even using a different name
> for 'g' the second time. 


But this is not ok for my purpose :-)

Anyway, thank you very much for your help Michele

Regards

Salvatore














More information about the Python-list mailing list