我正试图为inspect.getmembers方法编写一个谓词函数,以便可以获得对象中的属性列表。在
但我似乎找不到一种方法来获取传递给谓词函数的任何对象的名称。在下面的示例中,我可以看到传递给函数的对象类型In [12]: from __future__ import print_function
In [13]: inspect.getmembers('', lambda x: print(x))
str(object='') -> string
Return a nice string representation of the object.
If the argument is a string, the return value is the same object.
...
In [14]: inspect.getmembers('', lambda x: print(type(x)))
...
但我不知道如何从对象中获取名称,它似乎没有公开任何常规方法
^{pr2}$
如何从exampleMethod或传递到谓词的任何其他类型中获取__ne__?exampleMethod.__name__适用于该示例,但不适用于所有类型In [23]: inspect.getmembers('', lambda x: print(x.__name__))
__add__
str
__contains__
__delattr__
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
in ()
----> 1 inspect.getmembers('', lambda x: print(x.__name__))
C:\Anaconda\lib\inspect.pyc in getmembers(object, predicate)
254 except AttributeError:
255 continue
--> 256 if not predicate or predicate(value):
257 results.append((key, value))
258 results.sort()
in (x)
----> 1 inspect.getmembers('', lambda x: print(x.__name__))
AttributeError: 'str' object has no attribute '__name__'