我正在尝试为inspect.getmembers方法编写一个谓词函数,以便我可以获取对象中的属性列表。
但我似乎无法找到一种方法来获取传递给谓词函数的任何对象的名称。在下面的示例中,我可以看到传递给函数的对象的类型
In [12]: from __future__ import print_function
In [13]: inspect.getmembers('', lambda x: print(x))
<method-wrapper '__add__' of str object at 0x0000000001D67148>
<type 'str'>
<method-wrapper '__contains__' of str object at 0x0000000001D67148>
<method-wrapper '__delattr__' of str object at 0x0000000001D67148>
str(object='') -> string
Return a nice string representation of the object.
If the argument is a string, the return value is the same object.
<method-wrapper '__eq__' of str object at 0x0000000001D67148>
<built-in method __format__ of str object at 0x0000000001D67148>
<method-wrapper '__ge__' of str object at 0x0000000001D67148>
<method-wrapper '__getattribute__' of str object at 0x0000000001D67148>
<method-wrapper '__getitem__' of str object at 0x0000000001D67148>
<built-in method __getnewargs__ of str object at 0x0000000001D67148>
<method-wrapper '__getslice__' of str object at 0x0000000001D67148>
...
In [14]: inspect.getmembers('', lambda x: print(type(x)))
<type 'method-wrapper'>
<type 'type'>
<type 'method-wrapper'>
<type 'method-wrapper'>
<type 'str'>
<type 'method-wrapper'>
<type 'builtin_function_or_method'>
<type 'method-wrapper'>
<type 'method-wrapper'>
<type 'method-wrapper'>
<type 'builtin_function_or_method'>
<type 'method-wrapper'>
...
但我不知道如何从该对象中获取名称,它似乎没有公开任何正常的方法
In [15]: exampleMethod = inspect.getmembers('')[20]
In [17]: print(exampleMethod)
('__ne__', <method-wrapper '__ne__' of str object at 0x0000000001D67148>)
In [18]: exampleMethod = exampleMethod[1]
In [19]: print(exampleMethod)
<method-wrapper '__ne__' of str object at 0x0000000001D67148>
In [20]: dir(exampleMethod)
Out[20]:
['__call__',
'__class__',
'__cmp__',
'__delattr__',
'__doc__',
'__format__',
'__getattribute__',
'__hash__',
'__init__',
'__name__',
'__new__',
'__objclass__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__self__',
'__setattr__',
'__sizeof__',
'__str__',
'__subclasshook__']
In [21]: exampleMethod.name
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-21-09eecffac362> in <module>()
----> 1 exampleMethod.name
AttributeError: 'method-wrapper' object has no attribute 'name'
如何__ne__
从 exampleMethod 或传递给谓词的任何其他类型中获取?exampleMethod.__name__
适用于该示例,但不适用于所有类型
In [23]: inspect.getmembers('', lambda x: print(x.__name__))
__add__
str
__contains__
__delattr__
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-23-0e3fe501926e> in <module>()
----> 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()
<ipython-input-23-0e3fe501926e> in <lambda>(x)
----> 1 inspect.getmembers('', lambda x: print(x.__name__))
AttributeError: 'str' object has no attribute '__name__'