forked from pynutshell/pynut4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsingleton.py
57 lines (43 loc) · 1.7 KB
/
singleton.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
class Singleton:
_singletons = {}
def __new__(cls, *args, **kwds):
if cls not in cls._singletons:
cls._singletons[cls] = obj = super().__new__(cls)
obj._initialized = False
return cls._singletons[cls]
# Singleton example - apologies to J.R.R. Tolkien
class RingOfPower:
def __init__(self, ring_owner):
self.owner = ring_owner
self.bound_rings = []
def bind_in_the_darkness(self):
return self.bound_rings[:]
def rules(self, rings):
self.bound_rings.extend(rings)
def __str__(self):
return f'{id(self)}: {self.owner}'
class TheOneRing(Singleton, RingOfPower):
def __init__(self):
# guard against re-init on subsequent reference to singleton
if self._initialized:
return
self._initialized = True
super().__init__('The Dark Lord on His Dark Throne')
# The Rings of Power are created
elven_rings = [RingOfPower('Elven King Under The Sky') for _ in range(3)]
dwarven_rings = [RingOfPower('Dwarf Lord in Their Hall of Stone') for _ in range(7)]
mortal_men_rings = [RingOfPower('Mortal Man Doomed to Die') for _ in range(9)]
one_ring = TheOneRing()
# One Ring to rule them all
one_ring.rules(elven_rings)
one_ring.rules(dwarven_rings)
one_ring.rules(mortal_men_rings)
# list all the rings
for ring in one_ring.bind_in_the_darkness():
print(ring)
print(one_ring)
# try to create another One Ring
another = TheOneRing()
print('one_ring', id(one_ring), id(one_ring.bound_rings))
print('another ', id(another), id(another.bound_rings)) # same id as for one_ring, and same instance var
print('They are the same ring', another is one_ring) # prints True, they are the same object