Override used classes in parent class

Suppose a class exists NiceClassusing another class LesserClassin a place that I cannot edit

# NiceClass.py

class LesserClass:
    ...
    # stuff
    ...

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

Now I want to use my own class MyLesserClassinstead of LesserClasseverywhere in the legacy versionNiceClass

# 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 non-overlapping methods MyNiceClasswill be used LesserClassfrom the old NiceClass.py.

Everything will work the way I want if I just copied the whole definition NiceClassinto MyNiceClass.py.

I like to just inherit the source code, not the whole namespace. Maybe inheritance is wrong?

+4
1

, NiceClass, LesserClass.

, , MyNiceClass MyLesserClass LesserClass, __globals__ dict 'LesserClass' MyLesserClass.

, , __getattribute__:

class A:
    a = 'A.a'
    b = 'A.b'


class B:
    def func_a(self):
        print(A.a)

    def func_b(self):
        print(A.b)


class C:
    a = 'C.a'
    b = 'C.b'


class D(B):
    def func_a(self):
        print(C.a)

    def __getattribute__(self, attr):
        value = object.__getattribute__(self, attr)
        if callable(value):
            value = update_namespace(value, {'old': {'name': 'A', 'obj': A}, 'new': {'obj': C}})
        return value


def update_namespace(func, namespace):
    def wrapper(*args, **kwargs):
        # Update the globals
        func.__globals__[namespace['old']['name']] = namespace['new']['obj']
        val = func(*args, **kwargs)
        # Restore it back to the actual value
        func.__globals__[namespace['old']['name']] = namespace['old']['obj']
        return val
    return wrapper


d = D()
d.func_a()  # This should print C.a
d.func_b()  # This should print C.b

:

C.a
C.b
+2

Source: https://habr.com/ru/post/1683674/


All Articles