Is there a way to access __dict__ (or something like it) that includes base classes?

前端 未结 5 845
南旧
南旧 2020-12-20 12:42

Suppose we have the following class hierarchy:

class ClassA:

    @property
    def foo(self): return \"hello\"

class ClassB(ClassA):

    @property
    def         


        
5条回答
  •  一整个雨季
    2020-12-20 13:09

    You should use python's inspect module for any such introspective capabilities.

    .
    .
    >>> class ClassC(ClassB):
    ...     def baz(self):
    ...         return "hiya"
    ...
    >>> import inspect
    >>> for attr in inspect.getmembers(ClassC):
    ...   print attr
    ... 
    ('__doc__', None)
    ('__module__', '__main__')
    ('bar', )
    ('baz', )
    ('foo', )
    

    Read more about the inspect module here.

提交回复
热议问题