Singleton class | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula gr√°tis
+ 1

Singleton class

As i have created a example of singleton. Wanna ask if it is correct ? class Singleton: __instance=None def __init__(self): if Singleton.__instance!=None: raise Exception("This class is Singleton") else: Singleton.__instance=self obj=Singleton() #instance created obj1=Singleton() #exception error "This class is Singleton"

24th Jun 2018, 3:07 PM
apurvchaudhary© - avatar
2 Respostas
+ 1
I think it works. And depending on requirement, we can return the instance instead of raising an exception.
3rd Jul 2018, 3:05 AM
Dilip Srinivas
Dilip Srinivas - avatar
thanx buddy
3rd Jul 2018, 4:21 AM
apurvchaudhary© - avatar