0

我正在尝试为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__'
4

1 回答 1

1

你正在寻找__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__谓词不能访问属性名;谓词只能访问值,而不是存储它的属性名称,并且字符串永远不会有自己的名称。

于 2013-06-24T17:14:26.477 回答