I need to find a method on a super()
object by name. This works when using the "dot operator" notation: super(self.__class__,self).p
. However, I need to do that dynamically, something like super(self.__class__,self)["p"]
, which does not work.
I tried to use __getattr__
, but super object does not have that method. And super(self.__class__,self).__getattribute__("p")
returns self.p
instead super().p
.
How to do that correctly and elegantly?
For instance:
class A(object):
def p (self):
skip
class B(object):
def p (self):
skip
class AB (A,B):
def p (self):
skip
ab = AB()
o = super(ab.__class__, ab)
print dir(o)
print o.__getattribute__("p").__code__
print o.p.__code__
This code outputs:
['__class__', '__delattr__', '__doc__', '__format__', '__get__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__
', '__self__', '__self_class__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__thisclass__']
<code object p at 000000000205f2b0, file "b.py", line 10> # AB.p
<code object p at 0000000000361cb0, file "b.py", line 2> # A.p