Search code examples
python-3.xclassmultiple-inheritancemixinssuper

how make super class refer to itself


I want to make a mixin class refer to its own class during its init.

If I make it refer to self.__class__ then it refers to the class of the instance it is mixed into, not its own class. If it refers to mx.__class__ if refers to class 'type'.

class mx:
    def __init__(self):
        print( self.__class__ )
        print( mx.__class__ )

class C( mx ):
    def __init__(self):
        super().__init__()

>>> o = C()
<class '__main__.C'>
<class 'type'>

If, on the other hand, I create it as an instance of its own, it gets the class reference I seek when it refers to self.

>>> m = mx()
<class '__main__.mx'>
<class 'type'>

How can I get mx to refer to <class '... .mx'> from itself?


Solution

  • I want to make a mixin class refer to its own class during its init.

    The question is confusing because you are asking how to access the current class dynamically, but are actually expecting a static result. I would instead recommend using mx instead.