Advertisement
Guest User

Untitled

a guest
Dec 12th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import os
  3. import sys
  4.  
  5.  
  6. class BoundedMeta(type):
  7.  
  8. def __new__(mcs, name, bases, attrs, **kwargs):
  9. if 'max_count' in kwargs:
  10. mcs.max_count = kwargs['max_count']
  11. else:
  12. mcs.max_count=3
  13. return super().__new__(mcs, name, bases, attrs)
  14.  
  15. def __init__(cls, name, bases, attrs, **kwargs):
  16. super().__init__(name, bases, attrs)
  17. cls.max_count=__class__.max_count
  18.  
  19. def __call__(cls):
  20. if __class__.max_count == 0:
  21. raise TypeError
  22. __class__.max_count -= 1
  23. return super().__call__()
  24.  
  25.  
  26. class BoundedBase(metaclass=BoundedMeta):
  27.  
  28. def __new__(cls, *args, **kwargs):
  29. return super().__new__(cls, *args, **kwargs)
  30.  
  31. def __init__(self):
  32. pass
  33.  
  34. @classmethod
  35. def get_max_instance_count(cls):
  36. return cls.max_count
  37.  
  38.  
  39.  
  40. class myclass(BoundedBase):
  41.  
  42. def __init__(self):
  43. super().__init__()
  44. self.name=''
  45.  
  46.  
  47. c1 = myclass()
  48. c2 = myclass()
  49. item=5
  50. print(myclass.get_max_instance_count())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement