Search code examples
pythonsubclass

How do you delete an inherited function in a subclass?


My code basically looks like this

class main:
    def __init__(self):
        pass
    def unneededfunction(self):
        print("unneeded thing")

class notmain(main):
    def __init__(self):
        pass 
    #code for getting rid of unneededfunction here

How do you get rid of notmain.unneededfunction? (i.e., calling it results in an error)


Solution

  • If you don't want notmain to have unneededfunction then notmain should not be a subclass of main. It defeats the whole point of inheritance to fight the system this way.

    If you really insist on doing it, notmain could redefine unneededfunction and raise the same exception that would be raised if unneededfunction didn't exist, AttributeError. But again, you're going against the grain.

    Aside from that, you can't delete unneededfunction from notmain because notmain doesn't own that method, its parent class main does.