在pyton代码中,我有一个对象的绑定方法。只知道这个绑定的方法,我想知道该对象的类是什么。可能吗?如何知道类的名称,如果我只知道python中的绑定方法?
下面是示例代码:
>>> class x:
... def method(self):
... pass
...
>>> x
<class __main__.x at 0xb737d1ac>
>>> x_instance = x()
>>> x_instance
<__main__.x instance at 0xb737c7cc>
>>> boundmethod = x_instance.method
>>> boundmethod
<bound method x.method of <__main__.x instance at 0xb737c7cc>>
>>> str(boundmethod)
'<bound method x.method of <__main__.x instance at 0xb737c7cc>>'
假设我只知道boundmethod
。如何确定课程是x
?
考虑看看[这](http://stackoverflow.com/questions/4679592/how-to-find-instance-of-a后在python中的方法)问题我知道我可以像'boundmethod.im_self .__ class __.__ name__'这样做。有没有更简单的方法? – 2013-04-10 11:40:59
为什么会有更简单的方法?你为什么传递绑定的方法而不是绑定的对象呢? – geoffspear 2013-04-10 11:43:03
看着jamylak评论我终于找到了解决方案:'boundmethod.im_class .__ name__'。谢谢。 – 2013-04-10 11:43:28