[PyQt] super()
Carlos Scheidegger
cscheid at sci.utah.edu
Thu Jul 19 16:42:00 BST 2007
>> I would like all the function calls to be consistent so that there's less
>> risks of future gotchas when changing classes to use mixins. Is multiple
>> inheritance in PyQt generally considered a bad idea? What are PyQt best
>> practices when it comes to mixins?
>
> http://www.riverbankcomputing.com/Docs/PyQt4/pyqt4ref.html#super-and-pyqt-classes
>
> Phil
Shame on me for not looking it up first. I'm sorry.
I have a question about that paragraph, though. It's not clear by reading the
docs whether this lazy lookup happens on a per-class level or a per-instance
level. I would think logically it should be on a per-class level, but it
doesn't seem that explicitly calling getattr() on the sip-wrapped class is
enough to trigger the lookup.
I tried to make it work with the following class:
class qt_super(object):
def __init__(self, class_, obj):
self._class = class_
self._obj = obj
def __getattr__(self, attr):
s = super(self._class, self._obj)
try:
meth = getattr(s, attr)
except AttributeError:
mro = type(self._obj).mro()
# This should force a sip method lookup.
for class_ in mro:
try:
getattr(class_, attr)
except AttributeError:
pass
s = super(self._class, self._obj)
meth = getattr(s, attr)
return meth
but the second call to getattr on the super object still fails. However, this
works:
class qt_super(object):
def __init__(self, class_, obj):
self._class = class_
self._obj = obj
def __getattr__(self, attr):
s = super(self._class, self._obj)
try:
return getattr(s, attr)
except AttributeError, e:
mro = type(self._obj).mro()
try:
ix = mro.index(self._class)
except ValueError:
raise TypeError("qt_super: obj must be an instance of class")
for class_ in mro[ix+1:]:
try:
unbound_meth = getattr(class_, attr)
return types.MethodType(unbound_meth, self._obj, class_)
except AttributeError:
pass
raise e
Is there a simple reason? I'm just trying to understand what happens on the
sip side so I don't have to be stupid in public anymore :) (Not that I can
help it, but hey)
Thanks a lot for your patience,
-carlos
More information about the PyQt
mailing list