我需要一些关于super()方法的帮助。 考虑这个例子:
class A:
def subMethod(self):
return "a"
def callMe(self):
print(self.subMethod())
class B(A):
def subMethod(self):
return "b"
def callMe(self):
print("Look at me")
super().callMe()
现在,如果我调用B.callMe()
,它会打印a
。我要打印b
我如何解决这个问题?有可能吗
至于细节,我是在MicroPython平台上使用这段代码的,所以我只限于使用一些高级库,因为它只包含几个标准lib
下面是一个真实的例子:
class UARTbase():
def readLine(self):
pass
def readWithSign(self):
c = self.readLine()
self.printDebug("raw")
self.printDebug(c)
c = c.decode()
self.printDebug("decoded")
self.printDebug(c)
self.writeNoAck(c)
return c
class UARTesp(UARTbase):
def disableTermDecorator(func):
def aFunc(self, *args, **kwargs):
return_value = None
if(self.__term_count == 0):
uos.dupterm(None, 1)
self.getUart().init(timeout=self.TIMEOUT)
self.__term_count = self.__term_count + 1
try:
return_value = func(self, *args, **kwargs)
except Exception as e:
self.printDebug("Error in wrapper")
self.printDebug(e)
self.__term_count = self.__term_count - 1
if(self.__term_count == 0):
self.getUart().init(timeout=0)
uos.dupterm(self.getUart(), 1)
return return_value
gc.collect()
return aFunc
@disableTermDecorator
def readLine(self, *args, **kwargs):
c = self.getUart().readline()
if(c is not None):
return c[:-2]
# for compatibilty
return b''
@disableTermDecorator
def readWithSign(self, *args, **kwargs):
super().readWithSign(*args, **kwargs)
如您所见,我只需要调用super,这样我就可以用这个特定的decorator包装方法
当我打电话给super().readWithSign()
时,我说c
是None
,无论如何,它不应该是
解决了。我忘记了臭名昭著的
return
来自super()
。 代码:相关问题 更多 >
编程相关推荐