Home > database >  Possible for a class to look down at subclass constructor?
Possible for a class to look down at subclass constructor?

Time:05-09

I need to change a class variable but I don't know if I can do it at runtime because I'm using third party open source software and I'm not very well experienced with pure python inheritance (the software I use provide a custom inheritance system) and python in general. My idea was to inherit the class and change the constructor, but then the objects are initialized with original software classname, so they are not initialized with my init..

I partially solved it by inheriting classes that are using the original software class name and override methods so now they use my classes, but I still cannot reach every method because some of them are static.

Full step about what I was trying to do can be found here Inherit class Worker on Odoo15

I think that I clould solve the problem if I can use something, like a decorator or something else, to tell parent classes to 'look down' to child class constructor when they are initialized. Is there a way to do that?

I will provide an example:

class A(object):

    def __init__(self):
        self.timeout = 0

    def print_class_info(self):
        print(f'Obj A timeout: {self.timeout}')


class B(A):

    def __init__(self):
        super().__init__()
        self.timeout = 10

    def print_class_info(self):
        print(f'Obj B timeout: {self.timeout}')


# Is it possible, somehow, to make obj_A use the init of B class
# even if the call to the class is on class A?

obj_A = A()
obj_B = B()

obj_A.print_class_info()
obj_B.print_class_info()

OUT:
Obj A timeout: 0
Obj B timeout: 10

Of course, situation is more complex in the real scenario so I'm not sure if I can simply access object A and setup the class variable, I think I would have to do it at runtime, probably need a server restart and I'm not even sure how to access objects at runtime, as I said I'm not very well experienced with pure python.

Maybe there is an easy way but I just don't see it or know it, is it possible to use a subclass constructor with a parent class call, basically?

CodePudding user response:

You can assign any attribute to a class, including a method. This is called monkey patching

# save the old init function
A.__oldinit__ = A.__init__

# create a new function that calls the old one
def custom_init(self):
    self.__oldinit__()
    self.timeout = 10

# overwrite the old function
# the actual old function will still exist because
# it's referenced as A.__oldinit__ as well
A.__init__ = custom_init

# optional cleanup
del custom_init
  • Related