Python: Find out the "Spouse" class?

So I have a problem. Let's say I have 2 parent classes. They both inherit from the master class. Then they are the parent classes for the child class. Is there any way to find out (let it be said that I am a father) in which class of mother am I "with a child?" I do not need a child to understand which class of mothers; I want the Father to understand which class of mother is.

I know this is a stupid example, but this is a simplified version of what I have to do elsewhere.

class Master(object):
    def __init__(self):
        self.troll()
        self.trell()

class Mother1(Master):
    def troll(self):
        print 'troll1'

class Mother2(Master):
    def troll(self):
        print 'troll2'

class Father(Master):
    def trell(self):
        print 'trell'
        print self.figure_out_spouse_class()

class Child1(Mother1, Father):
    pass

class Child2(Mother2, Father):
    pass

c = Child1() #should print 'Mother1'
c = Child2() #should print 'Mother2'

~
~
~
~

+5
source share
2 answers

You can use __bases__:

def figure_out_spouse_class(self):
    return [b.__name__ for b in self.__class__.__bases__ if b != Father]

( "", ).

+6

__bases__ , -

class Father(Master):
    def trell(self):
        print self.__class__.__bases__[0]
        super(self.__class__, self).troll()
+1

All Articles