Your situation is not entirely clear, so I'm just going to answer the question "Is there a way to check if a class has been defined/exists?"
Yes, there are ways to check if a class has been defined in the current scope. I'll go through a few.
1. It's Better to Ask Forgiveness Than Permission
Just try to use it!
try:
var = MyClass()
except NameError:
# name 'MyClass' is not defined
...
This is probably the most Pythonic method (aside from just being sure you have the class imported/defined).
2. Look Through Current Scope
Everything in the current scope is given by dir()
. Of course, this won't handle things that are imported! (Unless they are imported directly.)
if not 'MyClass' in dir():
# your class is not defined in the current scope
...
3. Inspect a Module's Contents
Perhaps you want to see if the class is defined within a specific module, my_module
. So:
import my_module
import inspect
if not 'MyClass' in inspect.getmembers(my_module):
# 'MyClass' does not exist in 'my_module'
...
Now, it's worth pointing out that if at any point you are using these sorts of things in production code, you're probably writing your code poorly. You should always know which classes are in scope at any given point. After all, that's why we have import statements and definitions. I'd recommend you clean up your question with more example code to receive better responses.