1

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)

noob10293
  • 17
  • 6
  • 4
    The point of subclassing is to *extend*, not restrict. If `notmain` doesn't support the functionality of `main`, then it doesn't deserve to be a subclass. See [Liskov substitution principle](https://en.wikipedia.org/wiki/Liskov_substitution_principle) – Silvio Mayolo Aug 08 '21 at 01:44
  • @silviomayolo , the code is simplified. notmain would have other functions, and main would have other functions. I just wanted to get rid of a function I wouldn't use for cleaness. – noob10293 Aug 08 '21 at 01:47
  • 3
    Yes, but why is there a subclass relationship when there isn't an "is-a" relationship between the two classes? If `notmain` is a subclass of `main` then every `notmain` should be able to function completely and entirely as `main`, and it can't do that if I can't call `unneededfunction` on it. – Silvio Mayolo Aug 08 '21 at 01:51
  • I want to use main as a template for notmain, and technically they should both be subclasses of a single class, but I'm too lazy to code that in. – noob10293 Aug 08 '21 at 01:54
  • 2
    "but I'm too lazy to code that in"...well then garbage in...garbage out. The main issue here is what should happen when a user calls: `notmain_instance.unneededfunction()`? Ideally that doesn't happen, but if it does what do you want to happen? – Mark Aug 08 '21 at 01:57
  • 2
    I think you already have your answer then. The way to do what's posed in the question is to have a common superclass. Think of it as investing in the future. Spend the time now doing the code properly, so that when you read this code six months down the line, you don't have to spend time then figuring out what horrible `__delattr__` shenanigans you tried to engage in to hack a bad solution together. – Silvio Mayolo Aug 08 '21 at 01:57
  • wait, there's a `__delattr__`? – noob10293 Aug 08 '21 at 02:03

2 Answers2

2

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.

sj95126
  • 6,520
  • 2
  • 15
  • 34
-1

Do you need to delete it? (ie, it throws an attribute error). Or can you get away with partial inheritance?

The first answer here should address the latter: How to perform partial inheritance

If you just want it to throw an error on call this should work:

class notmain(main):
    def __init__(self):
        pass 
    def unneededfunction(self):
        raise NotImplementedError("explanation")

Or you can try the Mixin methods discussed here in structuring the classes: Is it possible to do partial inheritance with Python?

  • I would want to delete it, but I wouldn't want to throw a notimplementederror, I would like something like a built in delete function thing – noob10293 Aug 08 '21 at 02:11