faskiat
faskiat

Reputation: 689

Storing part of an "import" statement in an array

This may be a slightly dumb question, but I want to know how to store the subclass of the exceptions class. Let me explain: I want to have an array like this:

excep_type = [ValueError, NameError, IoError, ...]

The reason why is I'm building a class that I'm going to which I'm going to be referring to these exceptions often. I know all these exceptions live in the exceptions module (see here), so how could I go about doing this?

EDIT: I'm sorry, I should've mention I want ALL the exceptions in an array. I could type them all out manually, but surely there must be a way to grab them all?

Upvotes: 0

Views: 57

Answers (1)

chaps
chaps

Reputation: 158

You could try to use introspection like this:

import exceptions

my_exceptions = [x for x in dir(exceptions) if not x.startswith('__')]

print my_exeptions

['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BufferError', 'BytesWarning', 'DeprecationWarning', 'EOFError', 'EnvironmentError', 'Exception', 'FloatingPointError', 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning', 'IndentationError', 'IndexError', 'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'NameError', 'NotImplementedError', 'OSError', 'OverflowError', 'PendingDeprecationWarning', 'ReferenceError', 'RuntimeError', 'RuntimeWarning', 'StandardError', 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning', 'ZeroDivisionError']

Upvotes: 1

Related Questions