Finding in which class an object's method comes from
Chris Angelico
rosuav at gmail.com
Thu Feb 4 03:36:19 EST 2016
On Thu, Feb 4, 2016 at 7:25 PM, ast <nomail at invalid.com> wrote:
> Hi
>
> Suppose we have:
>
> ClassC inherit from ClassB
> ClassB inherit from ClassA
> ClassA inherit from object
>
> Let's build an object:
>
> obj = ClassC()
>
> Let's invoke an obj method
>
> obj.funct()
>
> funct is first looked in ClassC, then if not found
> on ClassB, then ClassA then object
>
> But is there a command to know where funct is
> found ?
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?
ChrisA
More information about the Python-list
mailing list