小编Lea*_*oN.的帖子

python - 如何获取内部类的列表?

我正在编写一个小型 Python 应用程序,其中包含一些嵌套类,如下例所示:

class SuperBar(object):
    pass

class Foo(object):
    NAME = 'this is foo'

    class Bar(SuperBar):
        MSG = 'this is how Bar handle stuff'

    class AnotherBar(SuperBar):
        MSG = 'this is how Another Bar handle stuff'
Run Code Online (Sandbox Code Playgroud)

我使用嵌套类来创建某种层次结构,并提供一种干净的方法来实现解析器的功能。

在某些时候,我想创建一个内部类的列表。我想要以下输出:

[<class '__main__.Bar'>, <class '__main__.AnotherBar'>]
Run Code Online (Sandbox Code Playgroud)

问题是:以 pythonic 方式获取内部类列表的推荐方法是什么?

python metaclass inner-classes python-2.7 python-3.x

5
推荐指数
1
解决办法
6004
查看次数