MRO theory

Carl Banks pavlovevidence at gmail.com
Wed Apr 11 20:21:28 EDT 2007


On Apr 11, 3:26 pm, "Clarence" <clarence1... at gmail.com> wrote:
> No, there is no circularity. The problem is coming about in the JPype
> application. It creates a Python class to proxy any Java class or Java
> interface that the program uses (directly or indirectly). As far as
> the
> Java proxies go, when it creates a class it can have at most one
> superclass
> that is a proxy of a Java class, and arbitrarily many superclasses
> which
> are proxies of Java interfaces.
>
> The problem basically comes about because interfaces are not classes,
> but a class is being created to stand in for each one.

Yes.  In Java, because there's no possibility of conflicting methods,
interfaces can be listed in any order.  But when proxying Java classes
with Python, the order of interfaces becomes important.

> There is real Java code that is causing a class construction failure
> because of an inability to construct a consistent mro. I've tried
> several
> approaches to make it work, and by accident, sorting the list of
> interface
> proxy classes, using the default comparison, make it work.

It *might* not be an accident.

Because Java interfaces can be listed in any order, it's quite
possible that some interface hierarchies in Java violate Python's MRO
restrictions right out of the box.  I would guess that JPype deals
with this situation by applying a consistent ordering to of bases when
it creates a proxy class.  And it just might turn out that "order of
creation" is the sort key.  Or something else could be the key
(alphabetical by name?).  Regardless, sorted order just *might* be the
proper order to list bases to ensure MRO consistency.

Or I could be way off base.

I'd suggest that this is probably a question better answered on the
JPype mailing list, since they would have answers and not just
speculations.


> That made
> me
> wonder if the theory had something to say. Obviously, if class A is
> created
> before class B, then B cannot be a base class of A, hence the
> chronological
> order of class creation _might_ have something to do with creating
> acceptable
> or unacceptable mro's.

I doubt it "happens" to work that way.  If sorting the bases works in
general, it's probably deliberate.


Carl Banks




More information about the Python-list mailing list