Override used classes in parent class

Viewed 128

Suppose there is a class NiceClass using some other class LesserClass in a place I can't edit

# NiceClass.py

class LesserClass:
    ...
    # stuff
    ...

class NiceClass:
    ...
    # Lots of use of lesser class in here...
    ...

Now I want to use my own class MyLesserClass instead of LesserClass everywhere in an inherited version of NiceClass

# MyNiceClass.py

from NiceClass import NiceClass
from NiceClass import LesserClass as oldLesserClass

class LesserClass(oldLesserClass):
    ...
    # Some extra stuff
    ...

class MyNiceClass(NiceClass):
    ...
    # Everywhere LesserClass was used I now want to use MyLesserClass
    ...

But all the non-overridden methods in MyNiceClass will use the LesserClass from the old NiceClass.py.

Everything would work as I want if I just copy-pasted in the whole definition of NiceClass into MyNiceClass.py.

It's like I just want to inherit the source code and not the whole namespace. Maybe inheritence is the wrong way?

1 Answers
Related