By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,621 Members | 1,074 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,621 IT Pros & Developers. It's quick & easy.

don't understand MRO

P: n/a
Hi,

I have a subclassed PyQt class:

class Node(object):
def move(self, x,y): pass

class CRhomb(QCanvasPolygon, Node): pass

$ python
v2.4.1
CRhomb.mro() [<class '__main__.CRhomb'>, <class 'qtcanvas.QCanvasPolygon'>, <class
'qtcanvas.QCanvasPolygonalItem'>, <class 'qtcanvas.QCanvasItem'>, <class
'qt.Qt'>, <type 'sip.wrapper'>, <class '__main__.Node'>, <type 'object'>]
a = CRhomb()
a.move(1,2)


This executes also Node.move(a, 1,2)
Why?

Because even QCanvasItem.move delegates the call to the derived object? But
qt.Qt does not have a move() method... how does it get passed on to Node?

Thanks in advance,
Ciao
Uwe
Jul 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

"Uwe Mayer" <me*****@hadiko.de> wrote in message
news:d9**********@news2.rz.uni-karlsruhe.de...
I have a subclassed PyQt class:

class Node(object):
def move(self, x,y): pass

class CRhomb(QCanvasPolygon, Node): pass

$ python
v2.4.1
CRhomb.mro() [<class '__main__.CRhomb'>, <class 'qtcanvas.QCanvasPolygon'>, <class
'qtcanvas.QCanvasPolygonalItem'>, <class 'qtcanvas.QCanvasItem'>, <class
'qt.Qt'>, <type 'sip.wrapper'>, <class '__main__.Node'>, <type 'object'>]
For those who don't know, 'mro' stands for 'method resolution order'. The
method returns a list of classes (all except the first are base or super
classes of the first) in the order in which their dictionaries are searched
for method (or other attribute) names.
a = CRhomb()
a.move(1,2)
This executes also Node.move(a, 1,2)
Why?
In the absence of other information, I would presume that none of the other
classes have a move() method.
Because even QCanvasItem.move delegates the call to the derived object?
But
qt.Qt does not have a move() method... how does it get passed on to Node?


Are you sure that QCanvasItem has a move method? What results from
print qtcanvas.QCanvasItem.move # ?

If so, I would need to see its code to try to answer.

Terry J. Reedy

Jul 19 '05 #2

P: n/a
Thursday 23 June 2005 19:22 pm Terry Reedy wrote:

[...]
In the absence of other information, I would presume that none of the
other classes have a move() method.
move() is implemented in the class qtcanvas.QCanvasItem
I checked the pyqt sources and it is linked via sip to the C++ object file.
In C++, QCanvasItem.move is delegated to QCanvasItem.moveBy.

-- snip: C++ sources --
void QCanvasItem::move( double x, double y ){
moveBy( x-myx, y-myy );
}

void QCanvasItem::moveBy( double dx, double dy ){
if ( dx || dy ) {
removeFromChunks();
myx += dx;
myy += dy;
addToChunks();
}
}
-- snip --
Are you sure that QCanvasItem has a move method? What results from
print qtcanvas.QCanvasItem.move # ? If so, I would need to see its code to try to answer.

import qtcanvas
qtcanvas.QCanvasItem.move

<built-in function move>

Here is a working portion which recreates the strange output:

-- snip --
from qtcanvas import *

class Node(object):
def move(self, x,y):
print "Node: move(%d,%d)"%(x,y)

class Rhomb(QCanvasPolygon, Node):
def __init__(self, parent):
QCanvasPolygon.__init__(self, parent)
Node.__init__(self)

print Rhomb.mro()
r = Rhomb(None)
r.move(1,2)
-- snip --

This prints:

[<class '__main__.Rhomb'>, <class 'qtcanvas.QCanvasPolygon'>, <class
'qtcanvas.QCanvasPolygonalItem'>, <class 'qtcanvas.QCanvasItem'>, <class
'qt.Qt'>, <type 'sip.wrapper'>, <class '__main__.Node'>, <type 'object'>]
Node: move(1,2)

Ciao
Uwe

Jul 19 '05 #3

P: n/a
Uwe Mayer wrote:
Thursday 23 June 2005 19:22 pm Terry Reedy wrote:

[...]
In the absence of other information, I would presume that none of the
other classes have a move() method.

move() is implemented in the class qtcanvas.QCanvasItem
I checked the pyqt sources and it is linked via sip to the C++ object file.
In C++, QCanvasItem.move is delegated to QCanvasItem.moveBy.

-- snip: C++ sources --
void QCanvasItem::move( double x, double y ){
moveBy( x-myx, y-myy );
}

void QCanvasItem::moveBy( double dx, double dy ){
if ( dx || dy ) {
removeFromChunks();
myx += dx;
myy += dy;
addToChunks();
}
}


I wonder if it is to do with the signature of these methods. they
accept two doubles and perhaps the python bindings do not automatically
convert from integers, therefore these methods are not called and the
rules of mro kick in (thus calling the python move method)


-- snip --

Are you sure that QCanvasItem has a move method? What results from
>print qtcanvas.QCanvasItem.move # ?


If so, I would need to see its code to try to answer.


import qtcanvas
qtcanvas.QCanvasItem.move


<built-in function move>

Here is a working portion which recreates the strange output:

-- snip --
from qtcanvas import *

class Node(object):
def move(self, x,y):
print "Node: move(%d,%d)"%(x,y)

class Rhomb(QCanvasPolygon, Node):
def __init__(self, parent):
QCanvasPolygon.__init__(self, parent)
Node.__init__(self)

print Rhomb.mro()
r = Rhomb(None)
r.move(1,2)
-- snip --

This prints:

[<class '__main__.Rhomb'>, <class 'qtcanvas.QCanvasPolygon'>, <class
'qtcanvas.QCanvasPolygonalItem'>, <class 'qtcanvas.QCanvasItem'>, <class
'qt.Qt'>, <type 'sip.wrapper'>, <class '__main__.Node'>, <type 'object'>]
Node: move(1,2)

Ciao
Uwe


Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.