这是可能的,但不推荐。
'method-wrapper'
是一种实现类型,因此特定于 cPython。
Python 2.7.5+ (default, Sep 19 2013, 13:49:51)
[GCC 4.8.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> object().__str__
<method-wrapper '__str__' of object object at 0xb74dd4d0>
>>> type(object().__str__)
<type 'method-wrapper'>
好吧,但是...
Python 2.7.3 (2.0.2+dfsg-4, Jun 28 2013, 11:25:07)
[PyPy 2.0.2 with GCC 4.8.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
And now for something completely different: ``"that's however just engineering"
(fijal)''
>>>> object().__str__
<bound method object.__str__ of <object object at 0xb6bd29e8>>
>>>> type(object().__str__)
<type 'method'>
因此,为了强调这种类型检查的想法有多糟糕,一些示例代码:
class T(object):
def __init__(self, val):
self.val = val
def thing(self):
pass
我在 cPython 中执行以下操作:
>>> isinstance(T(1).thing,type(object().__str__))
False
现在在pypy中:
>>>> isinstance(T(1).thing,type(object().__str__))
True
所以。如果您提供了有关您要完成的工作的更多详细信息,那可能会有所帮助。否则我给你留下上面的警示故事。