How to get name of class if I know only a bound method in python? [duplicate]

StackOverflow https://stackoverflow.com/questions/15924772

  •  03-04-2022
  •  | 
  •  

سؤال

In pyton code I've got a bound method of some object. Knowing only this bound method I would like to know what is the class of that object. Is it possible?

Here is sample code:

>>> 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>>'

Let's assume I know only boundmethod. How to determine that the class is x?

هل كانت مفيدة؟

المحلول

If you want the name of it:

boundmethod.im_class.__name__

Or in python 3:

boundmethod.__self__.__class__.__name__
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top