Singleton class | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 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©
apurvchaudhary© - avatar
2 Réponses
+ 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
0
thanx buddy
3rd Jul 2018, 4:21 AM
apurvchaudhary©
apurvchaudhary© - avatar