pyside - How to make super() work in this non-ideal situation in python? -
class clsone(object): def __init__(self): super(clsone, self).__init__() print "here's one" class clstwo(clsone): def __init__(self): super(clstwo, self).__init__() print "here's two" class clsthree(clstwo): # refer 1 blackbox object def __init__(self): # super(clsthree, self).__init__() print "here's three" class clsthreee(clstwo): # refer custom object def __init__(self): super(clsthreee, self).__init__() print "here's threee" class clsfour(clsthree, clsthreee): # multiple inheritance def __init__(self): super(clsfour, self).__init__() print "here's four" entity = clsfour() in case, trying combine clsthree (which comes single compiled library , hard change) , own clsthreee object together. because clsthree forgets call super() in constructor, kid cannot execute clsthreee's constructor when uses super().
as result, output this:
here's 3 here's 4 obviously, can manually call every bases of clsfour rather use super(), it's bit complicated when problem scattered on codebase.
by way, blackbox stuff pyside :)
supplement:
thanks @willemvanonsem , @raymondhettinger, previous clsfour question solved. further investigations, found similar problem in pyside doesn't have same concept.
in clsfour context, if try run:
print super(clsfour, self).__init__ you'll get:
<bound method clsfour.__init__ of <__main__.clsfour object @ 0x00000000031ec160>> but in following pyside context:
import sys pyside import qtgui class myobject(object): def __init__(self): super(myobject, self).__init__() print "here's myobject" class mywidget(qtgui.qwidget, myobject): def __init__(self): super(mywidget, self).__init__() app = qtgui.qapplication(sys.argv) widget = mywidget() print super(mywidget, widget).__init__ the result is:
<method-wrapper '__init__' of mywidget object @ 0x0000000005191d88> it doesn't print "here's myobject" , init attribute of super() has different type well. previously, try simplify problem clsfour. think isn't totally same.
i guess problem occurs in shiboken library i'm not sure.
tips:
the problem appear in pyqt context, can make myobject inherit qobject solve. solution useless in pyside.
super() proxy object uses method resolution order (mro) determine method call when perform call on super().
if inspect __mro__ of classfour, get:
>>> clsfour.__mro__ (<class '__main__.clsfour'>, <class '__main__.clsthree'>, <class '__main__.clsthreee'>, <class '__main__.clstwo'>, <class '__main__.clsone'>, <type 'object'>) or made shorter myself (not python output):
>>> clsfour.__mro__ (clsfour, clsthree, clsthreee, clstwo, clsone, object) now super(t,self) proxy object uses mro (but excluding) t. means super(clsfour,self) proxy object works with:
(clsthree, clsthreee, clstwo, clsone, object) # super(clsfour,self) what happen if query attribute (a method attribute) of class python walk through mro , inspect whether element has such attribute. first inspect whether clsthree has __init__ attribute, if not continue in clsthreee , on. moment finds such attribute stop, , return it.
so super(clsfour,self).__init__ return clsthree.__init__ method. mro used find methods, attributes, etc. not defined on class level. if use self.x , x not attribute of object nor of clsfour object, again walk through mro in search x.
if want call __init__ of all direct parents of classfour can use:
class clsfour(clsthree, clsthreee): def __init__(self): # call *all* *direct* parents __init__ par in clsfour.__bases__: par.__init__(self) which elegant, since if bases change, still work. note have make sure __init__ exists every parent. since defined @ object level, can safely assume this. other attributes however, can not make assumption.
edit: mind result super() not necessary points parents, grandparents and/or ancestors of class. parent classes of object.
the super(clsthree,self) in clsthree class, - given clsfour object, work same mro (since takes mro self). super(clsthree,self) inspect following sequence of classes:
(clsthreee, clstwo, clsone, object) for instance, if write (out of scope of class) super(clstwo,entity).__init__(), get:
>>> super(clstwo,entity).__init__() here's 1 >>> super(clsthree,entity).__init__() here's 2 here's threee >>> super(clsthreee,entity).__init__() here's 2 >>> super(clsfour,entity).__init__() here's 3
Comments
Post a Comment