我想在Python中继承一个方法的子方法。有人能帮我弄清楚如何做到这一点吗?继承Python中的子方法
的是我想要做的例子:
class A(object):
def method(self, val):
def submethod():
return "Submethod action"
if not val:
return submethod()
return "Method action"
a = A()
class B(A):
def method(self, val):
#inherit submethod ?
def submethod():
return "Another submethod action"
return super(B,self).method(val)
b = B()
print "A : "
print a.method(True)
>> Method action
print a.method(False)
>> Submethod action
print "B : "
print b.method(True)
>> Method Action
print b.method(False)
Actual answer :
>> Submethod Action
**Wanted answer :
>> Another submethod action**
亲切的问候,
昆汀
你不能这样做(没有技巧)。为什么不把它定义为在课堂上的常规方法? –