inspect.getmembers(object[, predicate]): 返回一个对象的所有成员,返回形式是一个列表,列表中每个元素是一个二元组,分别为成员名称及成员内容。
下面以一个函数为例,演示一下都有哪些成员。 注意,__builtins__
和__globals__
两个特殊属性里有非常多与函数本身无关的内容,占用行数太多,所以没有打印。
import inspectdef add(a: int=10, b:int=20, *, c=30):return a+b+cret = inspect.getmembers(add)
print(type(ret))
print(len(ret))print('-' * 25, '分隔线', '-' * 25)for item in ret:if item[0] in ('__builtins__', '__globals__'):continueprint(item)print('-' * 25, '分隔线', '-' * 25)
print(len(dir(add)))for item in dir(add):print(item)print('-' * 25, '分隔线', '-' * 25)
print(f'{vars(add) = }')print('-' * 25, '分隔线', '-' * 25)
print(f'{add.__annotations__ = }')
print(f'{add.__defaults__ = }')
输出结果:
<class 'list'>
37
------------------------- 分隔线 -------------------------
('__annotations__', {'a': <class 'int'>, 'b': <class 'int'>})
('__call__', <method-wrapper '__call__' of function object at 0x0000027C6F0904A0>)
('__class__', <class 'function'>)
('__closure__', None)
('__code__', <code object add at 0x0000027C6F0E9140, file "F:\RolandWork\PythonProjects\studyPython\forTest.py", line 3>)
('__defaults__', (10, 20))
('__delattr__', <method-wrapper '__delattr__' of function object at 0x0000027C6F0904A0>)
('__dict__', {})
('__dir__', <built-in method __dir__ of function object at 0x0000027C6F0904A0>)
('__doc__', None)
('__eq__', <method-wrapper '__eq__' of function object at 0x0000027C6F0904A0>)
('__format__', <built-in method __format__ of function object at 0x0000027C6F0904A0>)
('__ge__', <method-wrapper '__ge__' of function object at 0x0000027C6F0904A0>)
('__get__', <method-wrapper '__get__' of function object at 0x0000027C6F0904A0>)
('__getattribute__', <method-wrapper '__getattribute__' of function object at 0x0000027C6F0904A0>)
('__getstate__', <built-in method __getstate__ of function object at 0x0000027C6F0904A0>)
('__gt__', <method-wrapper '__gt__' of function object at 0x0000027C6F0904A0>)
('__hash__', <method-wrapper '__hash__' of function object at 0x0000027C6F0904A0>)
('__init__', <method-wrapper '__init__' of function object at 0x0000027C6F0904A0>)
('__init_subclass__', <built-in method __init_subclass__ of type object at 0x00007FF8BE5F4290>)
('__kwdefaults__', {'c': 30})
('__le__', <method-wrapper '__le__' of function object at 0x0000027C6F0904A0>)
('__lt__', <method-wrapper '__lt__' of function object at 0x0000027C6F0904A0>)
('__module__', '__main__')
('__name__', 'add')
('__ne__', <method-wrapper '__ne__' of function object at 0x0000027C6F0904A0>)
('__new__', <built-in method __new__ of type object at 0x00007FF8BE5F4290>)
('__qualname__', 'add')
('__reduce__', <built-in method __reduce__ of function object at 0x0000027C6F0904A0>)
('__reduce_ex__', <built-in method __reduce_ex__ of function object at 0x0000027C6F0904A0>)
('__repr__', <method-wrapper '__repr__' of function object at 0x0000027C6F0904A0>)
('__setattr__', <method-wrapper '__setattr__' of function object at 0x0000027C6F0904A0>)
('__sizeof__', <built-in method __sizeof__ of function object at 0x0000027C6F0904A0>)
('__str__', <method-wrapper '__str__' of function object at 0x0000027C6F0904A0>)
('__subclasshook__', <built-in method __subclasshook__ of type object at 0x00007FF8BE5F4290>)
------------------------- 分隔线 -------------------------
37
__annotations__
__builtins__
__call__
__class__
__closure__
__code__
__defaults__
__delattr__
__dict__
__dir__
__doc__
__eq__
__format__
__ge__
__get__
__getattribute__
__getstate__
__globals__
__gt__
__hash__
__init__
__init_subclass__
__kwdefaults__
__le__
__lt__
__module__
__name__
__ne__
__new__
__qualname__
__reduce__
__reduce_ex__
__repr__
__setattr__
__sizeof__
__str__
__subclasshook__
------------------------- 分隔线 -------------------------
vars(add) = {}
------------------------- 分隔线 -------------------------
add.__defaults__ = (10, 20)
add.__annotations__ = {'a': <class 'int'>, 'b': <class 'int'>}
可以看出,函数返回的成员与使用dir函数返回的成员数量相同。
下面看一下一个类的成员
import inspectclass Foo:'''这是一个类'''prop1 = 123def m1(self):print('m1 method')
''''''
ret = inspect.getmembers(Foo)
print(type(ret))
print(len(ret))print('-' * 25, '分隔线', '-' * 25)for item in ret:print(item)print('-' * 25, '分隔线', '-' * 25)
print(len(dir(Foo)))for item in dir(Foo):print(item)print('-' * 25, '分隔线', '-' * 25)
print(f'{vars(Foo) = }')
输出结果:
<class 'list'>
29
------------------------- 分隔线 -------------------------
('__class__', <class 'type'>)
('__delattr__', <slot wrapper '__delattr__' of 'object' objects>)
('__dict__', mappingproxy({'__module__': '__main__', '__doc__': '这是一个类', 'prop1': 123, 'm1': <function Foo.m1 at 0x0000024513D139C0>, '__dict__': <attribute '__dict__' of 'Foo' objects>, '__weakref__': <attribute '__weakref__' of 'Foo' objects>}))
('__dir__', <method '__dir__' of 'object' objects>)
('__doc__', '这是一个类')
('__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>)
('__getstate__', <method '__getstate__' 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 0x000002451395AB80>)
('__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 0x00007FF8BE5F8DF0>)
('__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 0x000002451395AB80>)
('__weakref__', <attribute '__weakref__' of 'Foo' objects>)
('m1', <function Foo.m1 at 0x0000024513D139C0>)
('prop1', 123)
------------------------- 分隔线 -------------------------
29
__class__
__delattr__
__dict__
__dir__
__doc__
__eq__
__format__
__ge__
__getattribute__
__getstate__
__gt__
__hash__
__init__
__init_subclass__
__le__
__lt__
__module__
__ne__
__new__
__reduce__
__reduce_ex__
__repr__
__setattr__
__sizeof__
__str__
__subclasshook__
__weakref__
m1
prop1
------------------------- 分隔线 -------------------------
vars(Foo) = mappingproxy({'__module__': '__main__', '__doc__': '这是一个类', 'prop1': 123, 'm1': <function Foo.m1 at 0x0000024513D139C0>, '__dict__': <attribute '__dict__' of 'Foo' objects>, '__weakref__': <attribute '__weakref__' of 'Foo' objects>})