Home > Software engineering >  Is there any way to 'overwrite' a function contained within a method of a super-class?
Is there any way to 'overwrite' a function contained within a method of a super-class?

Time:05-10

I'm trying to make multiple subclasses, all of which do something slightly different, but the 'core' of the method is the same. To try and solve this, I attempted to write a function in the parent class, then overwrite it in child classes. Is there a more obvious, simpler, solution that I am completely overlooking?


class SuperClass:
    def __init__(self):
        def func():
            self.val  = 1
            self.val2 = 0
        func()

class SubClass(SuperClass):
    def __init__(self):
        super().__init__()
        def func():
            self.val = 2
        func()

print(SubClass().val)

Returns:

2

print(SubClass().val2)

Returns:

0

Is there a way to make the extra val2 operation not occur?

CodePudding user response:

Except for disassembling and then creating anew you can't and then it's just the same as if writing the __init__() anew in the subclass. The reason why is because the function is created within the local scope of other function thus there's no way to access it until the main function actually executes -- where it then declares the nested function, its body and afterwards, even executes it.

More on that here. Not completely the same, but falls into the category or modifying the local scope.

Edit: Well, of course, if you don't mind the first execution, then yes, you can call it twice while the second execution, within your subclass' __init__() will be then your "overwritten" func. That however isn't overwriting anything, just duplicating. If the point is solely to modify self.val it'll work, yet self.val will be modified twice:

  • from the Superclass' __init__().func() to 1
  • from the Subclass' __init__().func() to 2

If the actions of Superclass' __init__() aren't required, simply skip super().__init__() and let only your subclass' func execute, then it'll be overwritten, however without any superclass' behavior defined in __init__() present.

Edit2: Instead of introducing nested functions use a proper class' method:

class Superclass:
    def helper(self):
        self.val = 1
    def __init__(self):
        self.helper()

class Subclass(Superclass):
    def helper(self):
        self.val = 2

print(Superclass().val)
print(Subclass().val)

CodePudding user response:

Since func is the thing you want to be modifiable, it should live at class level and it should be the thing overridden. The way you've written it, the class does a thing, then completely throws it away for another thing. This is not only wasteful of processing speed, its hard to maintain. Intermediate values from a superclass "func" may still stick around in the subclasses.

Make func the thing to override

class SuperClass:
    def __init__(self):
        self.func()

    def func(self):
        self.val = 1
        
class SubClass(SuperClass):

    def func(self):
        self.val = 2

print(SubClass().val)
  • Related