Finding in which class an object's method comes from

ast nomail at invalid.com
Thu Feb 4 03:54:02 EST 2016


"Chris Angelico" <rosuav at gmail.com> a écrit dans le message de 
news:mailman.43.1454574987.30993.python-list at python.org...

> You can see that by looking at the objects without calling them:
>
>>>> class A:
> ...  def a(self): pass
> ...
>>>> class B(A):
> ...  def b(self): pass
> ...
>>>> class C(B):
> ...  def c(self): pass
> ...
>>>> obj = C()
>>>> obj.a
> <bound method A.a of <__main__.C object at 0x7fb7bdfa57f0>>
>>>> obj.b
> <bound method B.b of <__main__.C object at 0x7fb7bdfa57f0>>
>>>> obj.c
> <bound method C.c of <__main__.C object at 0x7fb7bdfa57f0>>
>
> The information comes from the function's qualname:
>
>>>> obj.c.__func__.__qualname__
> 'C.c'
>
> Is that what you're hoping for?


It is strange but I dont have the same result that you:
(Python 3.4)

>>> class A:
 def a(self):pass

 >>> class B(A):
 def b(self):pass

 >>> class C(B):
 def c(self):pass

 >>> obj = C()

>>> obj.a
<bound method C.a of <__main__.C object at 0x02963F90>>

But with __qualname__ it works

>>> obj.a.__func__.__qualname__
'A.a'




More information about the Python-list mailing list