2018-09-27
When is that useful? e.g. A velocity controller class of dual arm robots (x: arm index).
Implementation †
Usage †
Define a class where this class is used as the meta class.
class X(object):
__metaclass__= TMultiSingleton
In X.__init__, the first argument should be the mode value (x).
In a function of X, use X._delete(self.x) to delete the instance (or reduce the reference count).
X can be instantiated like a usual class:
x1,x2,x3= X(1),X(1),X(2)
where x1 and x2 are the same instance (ids are the same) since they have the same mode value (1). x3 is a different instance.
Reference †
- https://python-3-patterns-idioms-test.readthedocs.io/en/latest/Singleton.html
- https://kiwamiden.com/singleton-in-python
- http://www.denzow.me/entry/2018/01/28/171416