Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.62 KB | None | 0 0
  1. # Singleton/SingletonPattern.py
  2.  
  3. class OnlyOne:
  4.     class __OnlyOne:
  5.         def __init__(self, arg):
  6.             self.val = arg
  7.         def __str__(self):
  8.             return repr(self) + self.val
  9.     instance = None
  10.     def __init__(self, arg):
  11.         if not OnlyOne.instance:
  12.             OnlyOne.instance = OnlyOne.__OnlyOne(arg)
  13.         else:
  14.             OnlyOne.instance.val = arg
  15.     def __getattr__(self, name):
  16.         return getattr(self.instance, name)
  17.  
  18. x = OnlyOne('sausage')
  19. print(x)
  20. y = OnlyOne('eggs')
  21. print(y)
  22. z = OnlyOne('spam')
  23. print(z)
  24. print(x)
  25. print(y)
  26. print(`x`)
  27. print(`y`)
  28. print(`z`)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement