I am attempting to use the __new__ method in the following code:
class SingletonExample(object):
super().__new__(cls, **kwargs)
I am attempting to use the __new__ method in the following code:
class SingletonExample(object):
_instance = None
def __new__(cls, **kwargs):
if cls._instance is None:
cls._instance = super().__new__(cls, **kwargs)
return cls._instance
def __init__(self, **kwargs) -> None:
our_attributes = ('h', 'x')
if kwargs is not None:
for k, v in kwargs.items():
if k in our_attributes:
setattr(self, k, v)
a = SingletonExample(h=1)
and I get the following result:
(PRV) jonathan@jfgdev:/PR$ python -m Library.Testing.test2
Traceback (most recent call last):
File "<frozen runpy>", line 198, in _run_module_as_main
File "<frozen runpy>", line 88, in _run_code
File "/mnt/ProgrammingRenaissance/Library/Testing/test2.py", line 16, in <module>
a = SingletonExample(h=1)
^^^^^^^^^^^^^^^^^^^^^
File "/mnt/ProgrammingRenaissance/Library/Testing/test2.py", line 6, in __new__
cls._instance = super().__new__(cls, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: object.__new__() takes exactly one argument (the type to instantiate)
I am quite puzzled as it looks as if this code will not work if the super-class is 'object'. Any suggestions on how to proceed?
I am attempting to use the __new__ method in the following code:
class SingletonExample(object):
_instance = None
def __new__(cls, **kwargs):
if cls._instance is None:
cls._instance = super().__new__(cls, **kwargs)
return cls._instance
def __init__(self, **kwargs) -> None:
our_attributes = ('h', 'x')
if kwargs is not None:
for k, v in kwargs.items():
if k in our_attributes:
setattr(self, k, v)
a = SingletonExample(h=1)
and I get the following result:
(PRV) jonathan@jfgdev:/PR$ python -m Library.Testing.test2
Traceback (most recent call last):
File "<frozen runpy>", line 198, in _run_module_as_main
File "<frozen runpy>", line 88, in _run_code
File "/mnt/ProgrammingRenaissance/Library/Testing/test2.py", line 16, in <module>
a = SingletonExample(h=1)
^^^^^^^^^^^^^^^^^^^^^
File "/mnt/ProgrammingRenaissance/Library/Testing/test2.py", line 6, in __new__
cls._instance = super().__new__(cls, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: object.__new__() takes exactly one argument (the type to instantiate)
I am quite puzzled as it looks as if this code will not work if the super-class is 'object'. Any suggestions on how to proceed?
The problem that I am facing is that when the superclass is not1 "object"
'object', theÿ__init__ method may well need arguments. I do not know how
to determine if the superclass is 'object'. For what it is worth, any attemptÿto use this with different argumentsÿshould return the initial singleton and ignore further attempts to create a second instance.
The problem is that if you are dealing with a library class, you may
haveÿtimes when the superclass is 'object' while at other times, with a different inheritance hierarchy, the superclass may need arguments. My thought is that the object class __new__ method should not choke on arguments, just ignore them.
When I talk about 'object', I am talking about the ultimate base class
of any inheritance hierarchy.ÿ have seen the class named 'object' called that.
On Sat, Feb 17, 2024 at 7:06 PM dn via Python-list....
<python-list@python.org <mailto:python-list@python.org>> wrote:
PS please reply to the list - there may be others who can learn....
from, or
contribute to, this conversation!
On 18/02/24 13:21, Jonathan Gossage wrote:
- perhaps someone knows a better/proper way to do this?
Suggested research: custom classes, ABCs, and meta-classes...
Sysop: | Tetrazocine |
---|---|
Location: | Melbourne, VIC, Australia |
Users: | 6 |
Nodes: | 8 (0 / 8) |
Uptime: | 45:48:27 |
Calls: | 45 |
Files: | 21,492 |
Messages: | 63,514 |