class P(object):
def __init__(self):
print('Parent')
@staticmethod
def M1():
print('parent Static')
@classmethod
def M2(cls):
print('parent class method')
def M3(self):
print('Instance Method')
class Q(P):
@staticmethod
def W1():
super(Q,Q).M3()##Here I am getting error
Q.W1()
TypeError: unbound method M3() must be called with Q instance as first argument (got nothing instead)
P.M3
is not static or a class method.
Notice the self
in the signature of the method:
def M3(self):
There is no way you can call it from W1 without having an instance of a P object.
What you are trying to do it similar to P.M3()
and that will not work.
From a Q staticmethod
, you can call other static/class methods in your base class, however, to call an instance method you need an instance
. A static method in Q does not provide an instance, so it will be unable to call instance methods in the base class.
There are many ways that you could use to call M3 but they will depend on what you really need. For instance:
class Q(P):
@staticmethod
def W1():
p = P()
p.M3()
@staticmethod
def W2(p):
p.M3()
Q.W1()
some_p = P()
Q.W2(some_p)
some_q = Q()
Q.W2(some_q)
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments