Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append code to inherited class method

How would you append to the method of an inherited object? Say, for example:

class ABeautifulClass(GoodClass):
    def __init__(self, **kw):
        # some code that will override inherited code
    def aNewMethod(self):
        # do something

Now I have inherited code from GoodClass, how would I append code to an inherited method. If I inherited code from GoodClass, how would I append to it, instead of basically removing it and rewriting it. Is this possible in Python?

like image 924
Zizouz212 Avatar asked Jun 07 '26 23:06

Zizouz212


1 Answers

Try using super

class ABeautifulClass(GoodClass):
    def __init__(self, **kw):
        # some code that will override inherited code
    def aNewMethod(self):
        ret_val = super().aNewMethod() #The return value of the inherited method, you can remove it if the method returns None
        # do something

Learn more about super here

like image 123
no_name Avatar answered Jun 10 '26 13:06

no_name