Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

2

print(SubClass().val2)

Returns:

0

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

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading