Python 2.5.2 (r252:60911, Jul 31 2008, 17:31:22)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import hashlib
>>> hd = hashlib.md5().hexdigest
>>> hd
<built-in method hexdigest of _hashlib.HASH object at 0x7f9492d96960>
>>> hd.__self__.__class__
<type '_hashlib.HASH'>
>>> hd2 = hd.__self__.__class__.hexdigest
>>> hd2
<method 'hexdigest' of '_hashlib.HASH' objects>
>>> hd2.__objclass__
<type '_hashlib.HASH'>
哦,是的,还有一件事:
>>> hd.im_class
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'builtin_function_or_method' object has no attribute 'im_class'
>>> hd2.im_class
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'method_descriptor' object has no attribute 'im_class'
从python 3.3开始,
.im_class
就消失了。您可以改用.__qualname__
。这是相应的政治公众人物:https://www.python.org/dev/peps/pep-3155/使用嵌套函数:
我不是一个Python专家,但是这个有用吗?
它似乎适用于绑定方法,但在您的情况下,您可能正在使用未绑定方法,在这种情况下,这可能更有效:
这是我用的测试:
哦,是的,还有一件事:
所以如果你想要防弹的东西,它也应该处理
__objclass__
和__self__
。但你的里程数可能会有所不同。https://docs.python.org/reference/datamodel.html#the-standard-type-hierarchy
相关问题 更多 >
编程相关推荐