オブジェクトのプロパティのリストを取得できるように、 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__'