如何从方法包装器、inspect.getmembers谓词的内置函数或方法类型中获取名称



我正试图为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'

如何从exampleMethod或传递到谓词中的任何其他类型中获取__ne__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:Anacondalibinspect.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__'

您正在查找__name__。它列在exampleMethod:的探索性输出中

>>> import inspect
>>> inspect.getmembers('')[20]
('__ne__', <method-wrapper '__ne__' of str object at 0x10bbb2508>)
>>> inspect.getmembers('')[20][1].__name__
'__ne__'

几乎所有成员都具有这种属性;唯一的例外是__doc__字符串:

>>> [getattr(o, '__name__', None) for n, o in inspect.getmembers('')]
['__add__', 'str', '__contains__', '__delattr__', None, '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

对于__doc__,谓词不能访问属性名称;谓词只能访问值,不能访问存储该值的属性名,字符串永远不会有自己的名称。

相关内容

  • 没有找到相关文章

最新更新