name of the class that contains the method code
Posted
by kdlp
on Stack Overflow
See other posts from Stack Overflow
or by kdlp
Published on 2010-05-06T14:12:07Z
Indexed on
2010/05/06
14:48 UTC
Read the original article
Hit count: 114
python
|introspection
I'm trying to find the name of the class that contains method code.
In the example underneath I use self.__class__.__name__
, but of course this returns the name of the class of which self is an instance and not class that contains the test()
method code. b.test()
will print 'B'
while I would like to get 'A'
.
I looked into the inspect
module documentation but did not find anything directly useful.
class A:
def __init__(self):
pass
def test(self):
print self.__class__.__name__
class B(A):
def __init__(self):
A.__init__(self)
a = A()
b = B()
a.test()
b.test()
© Stack Overflow or respective owner