I will try to simplify the case here, when overriding __new__
as below I don't know the correct way of calling super __new__
to do the job, am I doing it ok or is there other way of doing this?
super().__new__(cls)
is not producing correct results as I expected
I'm a python beginner, please be patient, I'm C++ fluent
import weakref
class A(str):
def __init__(self,s):self.a=s
class B(str):
def __init__(self,s):self.b=s
class P(A,B):
manifest=weakref.WeakValueDictionary()
def __new__(cls,a,b):
o=P.manifest.get(a+b)
if not o:
print(a,b,super(P,cls))
#i thought this first should be used because __init__ puts the values in
#and we index the manifest with parameters [a+b]
#o=super().__new__(cls)#produces unique results for all requests?!?
#so i called like this and it works (either with a or b)
o=super().__new__(cls,a)#why favoring a over b?
#o=super().__new__(cls,b)#why favoring b over a?
#o=super().__new__(cls,a,b)#its an error (of coz)
P.manifest[a+b]=o
print("using",id(o))
return o
def __init__(self,a,b):
A.__init__(self,a)
B.__init__(self,b)
p=P("a","b")
q=P("a","b")
r=P("a","x")
print(id(p),id(q),id(r))
__new__
in Python. – Halcombsuper()
in__new__
at all. Sure, it works, but why? – Gigue