import inspect class A(object): a = 10 @classmethod def get_attribute(cls): print('1', vars(cls)) print('2', dir(cls)) print('3', cls.__dir__(cls)) print('4', cls.__dict__) print('5', locals()) print('6', globals()) print('*' * 20) attr_ = inspect.getmembers(cls, lambda a: not inspect.ismethod(a)) print('1', attr_) attr_ = filter(lambda a: not a[0].startswith('__'), attr_) print('2', attr_) attr_ = list(attr_) print('3', attr_) class B(A): b = 20 if __name__ == '__main__': b_obj = B() b_obj.get_attribute()
输出: 1 {'__module__': '__main__', 'b': 20, '__doc__': None} 2 ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'a', 'b', 'get_attribute'] 3 ['__repr__', '__call__', '__getattribute__', '__setattr__', '__delattr__', '__init__', '__new__', 'mro', '__subclasses__', '__prepare__', '__instancecheck__', '__subclasscheck__', '__dir__', '__sizeof__', '__basicsize__', '__itemsize__', '__flags__', '__weakrefoffset__', '__base__', '__dictoffset__', '__mro__', '__name__', '__qualname__', '__bases__', '__module__', '__abstractmethods__', '__dict__', '__doc__', '__text_signature__', '__hash__', '__str__', '__lt__', '__le__', '__eq__', '__ne__', '__gt__', '__ge__', '__reduce_ex__', '__reduce__', '__subclasshook__', '__init_subclass__', '__format__', '__class__'] 4 {'__module__': '__main__', 'b': 20, '__doc__': None} 5 {'cls': <class '__main__.B'>} 6 {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x7fe80ce524f0>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': '/home/vubuntu/PycharmProjects/meiduo_project/test.py', '__cached__': None, 'inspect': <module 'inspect' from '/usr/lib/python3.8/inspect.py'>, 'A': <class '__main__.A'>, 'B': <class '__main__.B'>, 'b_obj': <__main__.B object at 0x7fe80cd93d00>} ******************** 1 [('__class__', <class 'type'>), ('__delattr__', <slot wrapper '__delattr__' of 'object' objects>), ('__dict__', mappingproxy({'__module__': '__main__', 'b': 20, '__doc__': None})), ('__dir__', <method '__dir__' of 'object' objects>), ('__doc__', None), ('__eq__', <slot wrapper '__eq__' of 'object' objects>), ('__format__', <method '__format__' of 'object' objects>), ('__ge__', <slot wrapper '__ge__' of 'object' objects>), ('__getattribute__', <slot wrapper '__getattribute__' of 'object' objects>), ('__gt__', <slot wrapper '__gt__' of 'object' objects>), ('__hash__', <slot wrapper '__hash__' of 'object' objects>), ('__init__', <slot wrapper '__init__' of 'object' objects>), ('__init_subclass__', <built-in method __init_subclass__ of type object at 0x2595e20>), ('__le__', <slot wrapper '__le__' of 'object' objects>), ('__lt__', <slot wrapper '__lt__' of 'object' objects>), ('__module__', '__main__'), ('__ne__', <slot wrapper '__ne__' of 'object' objects>), ('__new__', <built-in method __new__ of type object at 0x90a5a0>), ('__reduce__', <method '__reduce__' of 'object' objects>), ('__reduce_ex__', <method '__reduce_ex__' of 'object' objects>), ('__repr__', <slot wrapper '__repr__' of 'object' objects>), ('__setattr__', <slot wrapper '__setattr__' of 'object' objects>), ('__sizeof__', <method '__sizeof__' of 'object' objects>), ('__str__', <slot wrapper '__str__' of 'object' objects>), ('__subclasshook__', <built-in method __subclasshook__ of type object at 0x2595e20>), ('__weakref__', <attribute '__weakref__' of 'A' objects>), ('a', 10), ('b', 20)] 2 <filter object at 0x7fe80cbfb070> 3 [('a', 10), ('b', 20)]