Search code examples
pythonclassinheritancedetection

Is it possible to detect whether a class is being called directly by the class or indirectly by a base class?


I am making a program with some inherited classes but my program needs to know whether a class' functions are being called directly from the class or from a base class e.g.

class Letters():
    def letter(): ...
    ...

class ABC(Letters):
    def __init__(self):
        if from_base_class:
            raise Exception
    def A(): ...
    def B(): ...
    def C(): ...

==== terminal: ====

>>> myclass = ABC()
>>> myclass.letter()
[with all of the line numbers here]
Exception

How can I implement this into my code?


Solution

  • So did you mean to raise an exception?

    class Letters():
        def letter(self): ...
        ...
    
    class ABC(Letters):
        def letter(self):
            raise NotImplementedError