Answer the question
In order to leave comments, you need to log in
How to get a list of all classes from the current file?
For example, here's a file:
class A:
pass
class B:
pass
class C:
pass
def get_all_classes_in_file():
''' Как добраться до всех классов, которые в этом файле лежат?'''
import pyclbr
print(pyclbr.readmodule(__name__).keys())
Answer the question
In order to leave comments, you need to log in
You need to know the name of the module. For the main script (which you directly execute) it will be '__main__'. EMNIP this name can be learned from the magic variable __name__.
import sys
def enum_classes(module_name: str = __name__):
module = sys.modules[module_name]
return [v for v in vars(module).values() if type(v) is type and v.__module__ == module_name]
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question