Skip to content

Commit 2eeb09a

Browse files
committed
Fix
1 parent 8f8e250 commit 2eeb09a

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

src/singleton2.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
Singleton
44
"""
55

6-
class Singleton(type):
6+
7+
class Singleton1(type):
78
_instance = None
89

910
def __call__(cls, *args, **kwargs):
@@ -12,36 +13,36 @@ def __call__(cls, *args, **kwargs):
1213
return cls._instance
1314

1415

15-
class Singleton(type):
16+
class Singleton2(type):
1617
"""
1718
Singleton metaclass
1819
Based on Python Cookbook 3rd Edition Recipe 9.13
1920
Only one instance of a class can exist. Does not work with __slots__
2021
"""
2122

2223
def __init__(self, *args, **kw):
23-
super(Singleton, self).__init__(*args, **kw)
24+
super(Singleton2, self).__init__(*args, **kw)
2425
self.__instance = None
2526

2627
def __call__(self, *args, **kw):
2728
if self.__instance is None:
28-
self.__instance = super(Singleton, self).__call__(*args, **kw)
29+
self.__instance = super(Singleton2, self).__call__(*args, **kw)
2930
return self.__instance
3031

31-
3232

33-
34-
class DB(metaclass=Singleton):
33+
class DBMongo(metaclass=Singleton2):
34+
pass
35+
36+
class DBPostgres(metaclass=Singleton2):
3537
pass
3638

3739

3840
if __name__ == '__main__':
39-
s1 = DB()
40-
s2 = DB()
41-
s3 = DB()
41+
m1 = DBMongo()
42+
m2 = DBMongo()
4243

43-
print(id(s1))
44-
print(id(s2))
45-
print(id(s3))
44+
p1 = DBPostgres()
45+
p2 = DBPostgres()
4646

47-
print(id(s1) == id(s2) == id(s3))
47+
print(f"Mongo: {id(m1)} - {id(m2)} ", id(m1) == id(m2))
48+
print(f"Postgres: {id(p1)} - {id(p2)} ", id(p1) == id(p2))

0 commit comments

Comments
 (0)