Search code examples
pythonmultiple-inheritancetornadomixins

Python multiple inheritence issues


I'm trying to run python code that has an inheritence pattern like so:

A Object
|   /\
|  B C
|  | |
|  D E
 \ | /
  \|/
   F
A = Base Handler
B = OAuth2 Mixin
C = OAuth Mixin
D = Facebook Graph Mixin
E = Twitter Mixin

The function names in B, C, D, E overlap. D and E are mixins so they shouldn't be independently spawned. How can I resolve this so class F can make calls to specific mixins?


Solution

  • If you know the names of C and D ahead of time, you can just call their methods and pass self explicitly:

    class E(C, D):
        def do_c_thing(self):
            # Call C's version
            C.some_method(self, ...)
    
        def do_d_thing(self):
            # Call D's version
            D.some_method(self, ...)