Twice instanciation

W Isaac Carroll icarroll at pobox.com
Thu Jun 12 16:15:23 EDT 2003


Salvatore wrote:
> Hello,
> 
> Is it possible to avoid twice instanciation
> of the same variable ?
> 
> class Global:
>    def __init__(self,init=0):
>       self.value = init
> 
> g = Global() #instanciating one time ok
> g = Global() #instanciating two times would raise an exception

What do you mean by "instanciation" and "variable"?

If you are asking whether it's possible to prevent binding the same name 
a second time, then no, it's not possible.

If you're asking whether it's possible to raise an exception the second 
time a class constructor is called, then yes, it is. (Although raising 
an exception is not the usual way to handle this. Search for the 
Highlander (Singleton) or Borg (Monostate) pattern for more information.)

TTFN






More information about the Python-list mailing list