Message330500
I am not sure if the following is resolved by your proposal, I post it just in case:
The following code works:
1. class Singleton(object):
2. def __new__(cls, *args, **kwargs):
3. if not hasattr(cls, 'instance'):
4. cls.instance = super(Singleton, cls).__new__(cls)
5. cls.instance._init_pointer = cls.instance._init_properties
6. else:
7. cls.instance._init_pointer = lambda *args, **kwargs: None # do nothing
8. return cls.instance
9. def __init__(self, *args, **kwargs):
10. super(Singleton, self).__init__()
11. self._init_pointer(*args, **kwargs)
12. def _init_properties(self, tag):
13. self.info = tag
14. #
15. if __name__ == '__main__':
16. S1 = Singleton('I am S1')
17. print('S1 info is:' + S1.info)
18. S2 = Singleton('Am I S2?')
19. print('S2 info is:' + S2.info)
However if I change line 4 into this code (which works in Python 2 by the way):
cls.instance = super(Singleton, cls).__new__(cls, *args, **kwargs)
I get:
TypeError: object.__new__() takes no arguments
But if I change line 4 into this (no arguments as suggested):
cls.instance = super(Singleton, cls).__new__()
I get:
TypeError: object.__new__(): not enough arguments
Line 10 has the same issue when changed to:
super(Singleton, self).__init__(*args, **kwargs) |
|
Date |
User |
Action |
Args |
2018-11-27 10:22:16 | ppt000 | set | recipients:
+ ppt000, ncoghlan, serhiy.storchaka, CuriousLearner, xtreak |
2018-11-27 10:22:16 | ppt000 | set | messageid: <1543314136.56.0.788709270274.issue31506@psf.upfronthosting.co.za> |
2018-11-27 10:22:16 | ppt000 | link | issue31506 messages |
2018-11-27 10:22:16 | ppt000 | create | |
|