Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.85 KB | None | 0 0
  1. class SedlacekStorage(object):
  2.     """
  3.    Please note, that every SedlacekStorage subclass is a singleton. You can't
  4.    use sync & async versions of +SedlacekStorage in the same process.
  5.    """
  6.     _tornado_client = None
  7.     _asyncio_client = None
  8.  
  9.     def __new__(cls, *args, **kwargs):
  10.         if not cls._instance:
  11.             # protect parent from our specific args
  12.             kwargscopy = dict(kwargs)
  13.             for key in ['async', 'timeout']:
  14.                 if key in kwargscopy:
  15.                     del kwargscopy[key]
  16.             cls._instance = super(SedlacekStorage, cls) \
  17.                 .__new__(cls, *args, **kwargscopy)
  18.         return cls._instance
  19.  
  20.  
  21. class A(SedlacekStorage):
  22.     _instance = None
  23.  
  24.  
  25. class B(A):
  26.     pass
  27.  
  28.  
  29. a1 = A()
  30. print(A._instance, B._instance)
  31.  
  32. b1 = B()
  33. b2 = B()
  34.  
  35. print(b1 is b2)
  36. print(a1 is b2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement