Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
29
Task/Singleton/Python/singleton-2.py
Normal file
29
Task/Singleton/Python/singleton-2.py
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
import abc
|
||||
|
||||
class Singleton(object):
|
||||
"""
|
||||
Singleton class implementation
|
||||
"""
|
||||
__metaclass__ = abc.ABCMeta
|
||||
|
||||
state = 1 #class attribute to be used as the singleton's attribute
|
||||
|
||||
@abc.abstractmethod
|
||||
def __init__(self):
|
||||
pass #this prevents instantiation!
|
||||
|
||||
@classmethod
|
||||
def printSelf(cls):
|
||||
print cls.state #prints out the value of the singleton's state
|
||||
|
||||
#demonstration
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
a = Singleton() #instantiation will fail!
|
||||
except TypeError as err:
|
||||
print err
|
||||
Singleton.printSelf()
|
||||
print Singleton.state
|
||||
Singleton.state = 2
|
||||
Singleton.printSelf()
|
||||
print Singleton.state
|
||||
Loading…
Add table
Add a link
Reference in a new issue