1

Is there a way that I can pragmatically determine, either at runtime, or via static analysis, the method resolution order for a given python class?

Imagine I have a set of classes

class A():
    ...

class B(A):
    ...

class C(A):
    ...

class D(B, C):
    def print_mro(self):
         print("D, B, C, A")

I want to find out the C3 linearization for them is without having to root through the source code and try to determine the order by hand. I'm working on a django app where the views have lots of mixins that have been up to now rather haphazardly ordered. I need to make sure that they are in the right order though, and for security reasons I don't want to check my work by hand and risk a data leak. I also don't want to go through by hand and add a print statement to every mixin's dispatch method.

timthelion
  • 2,636
  • 2
  • 21
  • 30

2 Answers2

1

What about

class A():
    pass

class B(A):
   pass

class C(A):
    pass

class D(B, C):
    def print_mro(self):
         print(self.__class__.__mro__)

d = D()
d.print_mro()

?

-1

you can also do-

d = D()
print(help(d))
Pankaj Mishra
  • 445
  • 6
  • 15