Paolo Taddonio <[email protected]> added the comment:
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)
----------
nosy: +ppt000
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue31506>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com