0
import typing

class A:
    def __init__(self, symbol: str):
        if not symbol:
            raise self.MissingException('Symbol (str) required!')

        self.symbol = symbol

    class MissingException(Exception):
        pass

if __name__ == '__main__':
    A(None)

Output

Traceback (most recent call last):
  File "/tmp/test.py", line 16, in <module>
    A(None)
  File "/tmp/test.py", line 7, in __init__
    raise self.MissingException('Symbol (str) required!')
__main__.MissingException: Symbol (str) required!

Could someone please help me understand why is the Exception named __main__.MissingException instead of being named as __main__.A.MissingException?

Thanks

ngrj93
  • 1
  • 1
  • 1
    Relevant [what-is-the-purpose-of-pythons-inner-classes](https://stackoverflow.com/questions/719705) – stovfl May 01 '20 at 09:48
  • Consider class B which is a copy of class A in the same module. I would certainly prefer if the names were `__main__.A.MissingException` and `__main__.B.MissingException` instead of `__main__.MissingException` shown for both. – ngrj93 May 01 '20 at 09:57
  • I understand what you want to accomplish. Extended your error string with `self.__class__.__name__`. – stovfl May 01 '20 at 10:08
  • Thanks for the suggestion. Yes, that would certainly distinguish between the exception messages. However, my curiosity with why the above behaviour was favoured still stands. – ngrj93 May 01 '20 at 10:14

0 Answers0