Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import os
- import sys
- class BoundedMeta(type):
- def __new__(mcs, name, bases, attrs, **kwargs):
- if 'max_count' in kwargs:
- mcs.max_count = kwargs['max_count']
- else:
- mcs.max_count=3
- return super().__new__(mcs, name, bases, attrs)
- def __init__(cls, name, bases, attrs, **kwargs):
- super().__init__(name, bases, attrs)
- cls.max_count=__class__.max_count
- def __call__(cls):
- if __class__.max_count == 0:
- raise TypeError
- __class__.max_count -= 1
- return super().__call__()
- class BoundedBase(metaclass=BoundedMeta):
- def __new__(cls, *args, **kwargs):
- return super().__new__(cls, *args, **kwargs)
- def __init__(self):
- pass
- @classmethod
- def get_max_instance_count(cls):
- return cls.max_count
- class myclass(BoundedBase):
- def __init__(self):
- super().__init__()
- self.name=''
- c1 = myclass()
- c2 = myclass()
- item=5
- print(myclass.get_max_instance_count())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement