How do you get all classes defined in a module but not imported?

前端 未结 5 1745
忘掉有多难
忘掉有多难 2020-11-30 11:11

I\'ve already seen the following question but it doesn\'t quite get me where I want: How can I get a list of all classes within current module in Python?

In particul

5条回答
  •  执念已碎
    2020-11-30 11:12

    I used the below:

    # Predicate to make sure the classes only come from the module in question
    def pred(c):
        return inspect.isclass(c) and c.__module__ == pred.__module__
    # fetch all members of module __name__ matching 'pred'
    classes = inspect.getmembers(sys.modules[__name__], pred)
    

    I didn't want to type the current module name in

提交回复
热议问题