如何从python中相同的重载派生类方法中调用基类方法?
我在python项目中设置了以下类 在MicroSim.py中如何从python中相同的重载派生类方法中调用基类方法?,python,python-2.7,Python,Python 2.7,我在python项目中设置了以下类 在MicroSim.py中 class MicroSim: def __init__(self, other_values): # init stuff here def _generate_file_name(self, integer_value): # do some stuff here def run(self): # do some more stuff s
class MicroSim:
def __init__(self, other_values):
# init stuff here
def _generate_file_name(self, integer_value):
# do some stuff here
def run(self):
# do some more stuff
self._generate_file_name(i)
在thresholdsim.py中
from MicroSim import MicroSim
class ThresholdCollabSim (MicroSim):
# no __init__() implmented
def _generate_file_name(self, integer_value):
# do stuff here
super(ThresholdCollabSim, self)._generate_file_name(integer_value) # I'm trying to call MicroSim._generate_file_name() here
# run() method is not re-implemented in Derived!
在MicroSimRunner.py中
from ThresholdCollabSim import ThresholdCollabSim
def run_sims(values):
thresholdSim = ThresholdCollabSim(some_values) # I assume since ThresholdCollabSim doesn't have it's own __init__() the MicroSim.__init() will be used here
thresholdSim.run() # again, the MicroSim.run() method should be called here since ThresholdCollabSim.run() doesn't exist
当我运行此代码时,我得到错误消息
回溯(最后一次调用):文件“stdin”,第1行,在
运行模拟程序中第11行的文件“H:…\MicroSimRunner.py”
thresholdSim.run()文件“H:…\MicroSim.py”,第42行,在run中
self.\u generate\u file\u name(r)文件“H:…\ThresholdCollabSim.py”,第17行,在\u generate\u file\u name中
super(ThresholdCollabSim,self)。\u generate\u file\u name(curr\u run)类型错误:未绑定方法\u generate\u file\u name()必须为
以MicroSim实例作为第一个参数调用(get int instance
(取而代之)
我曾尝试在这里搜索类似的问题,并找到了类似的帖子,并尝试了这里讨论的所有解决方案,但这个错误似乎没有消失。我试着把这句话改成
super(ThresholdCollabSim, self)._generate_file_name(self, curr_run)
但它并没有改变什么(同样的错误)。我对Python编程比较陌生,所以这可能只是一个愚蠢的错误。非常感谢您的帮助。谢谢 您忘记了派生的
\u generate\u file\u name
方法中的self
参数。另外,您需要使用类MicroSim(object)
作为补充,使MicroSim成为一个新样式的类
在旧式类中使用super
从文件中,我们知道:
super()仅适用于新样式的类
一个新型的班级正在进行中
从对象继承的任何类
您需要使用
class MicroSim(object)
将MicroSim创建为一个新样式的类。thresholdcolpsim如何。generate\u file\u name(self,curr\u run)
?thresholdcolpsim.\u generate\u file\u name(self,curr\u run)
没有帮助。什么是新型课堂?让MicroSim成为新型课堂很有效。谢谢在我写问题的时候,遗漏了“自我”的拼写错误,它已经被修复了。但这并不能解决问题。什么是新型课堂?