TypeError:_slow_trap_ramp()需要1个位置参数,但是给出了2个
def demag_chip(self):
coil_probe_constant = float(514.5)
field_sweep = [50 * i * (-1)**(i + 1) for i in range(20, 0, -1)] #print as list
for j in field_sweep:
ramp = self._slow_trap_ramp(j)
def _set_trap_ramp(self):
set_trap_ramp = InstrumentsClass.KeysightB2962A.set_trap_ramp
return set_trap_ramp
def _slow_trap_ramp(self):
slow_trap_ramp = ExperimentsSubClasses.FraunhoferAveraging.slow_trap_ramp
return slow_trap_ramp
答案 0 :(得分:1)
看起来您的代码提取包含某些类的方法,其完整定义未显示,并且您从另一个方法(self._slow_trap_ramp(j)
)调用一个方法。当您调用方法时,Python会在任何其他参数之前自动传递self
。因此,您需要将def _slow_trap_ramp(self)
更改为def _slow_trap_ramp(self, j)
。
更新以回复评论
要真正提供帮助,我们需要查看您正在编写的更多课程,以及您正在调用的其他对象的一些信息。但是我会想出你的代码看起来像这样:
<强> InstrumentsClass.py 强>
class KeysightB2962A
def __init__(self):
...
def set_trap_ramp(self):
...
<强> ExperimentsSubClasses.py 强>
class FraunhoferAveraging
def __init__(self):
...
def slow_trap_ramp(self, j):
...
当前版本的main.py
import InstrumentsClass, ExperimentsSubClasses
class MyClass
def __init__(self)
...
def demag_chip(self):
coil_probe_constant = float(514.5)
field_sweep = [50 * i * (-1)**(i + 1) for i in range(20, 0, -1)] #print as list
for j in field_sweep:
ramp = self._slow_trap_ramp(j)
def _set_trap_ramp(self):
set_trap_ramp = InstrumentsClass.KeysightB2962A.set_trap_ramp
return set_trap_ramp
def _slow_trap_ramp(self):
slow_trap_ramp = ExperimentsSubClasses.FraunhoferAveraging.slow_trap_ramp
return slow_trap_ramp
if __name__ == "__main__":
my_obj = MyClass()
my_obj.demag_chip()
如果是这种情况,那么这些是主要问题:
self
和j
传递给MyClass._slow_trap_ramp
,但您只是将其定义为接受self
(如上所述),KeysightB2962A
和FraunhoferAveraging
中的类方法,而不是实例化类并使用实例的方法,您可以通过将代码更改为这样来修复所有这些问题(请参阅嵌入式注释):
新版本的main.py
import InstrumentsClass, ExperimentsSubClasses
class MyClass
def __init__(self)
# create instances of the relevant classes (note parentheses at end)
self.keysight = InstrumentsClass.KeysightB2962A()
self.fraun_averaging = ExperimentsSubClasses.FraunhoferAveraging()
def demag_chip(self):
coil_probe_constant = float(514.5)
field_sweep = [50 * i * (-1)**(i + 1) for i in range(20, 0, -1)] #print as list
for j in field_sweep:
ramp = self._slow_trap_ramp(j)
def _set_trap_ramp(self):
# call instance method (note parentheses at end)
return self.keysight.set_trap_ramp()
def _slow_trap_ramp(self, j): # accept both self and j
# call instance method (note parentheses at end)
return self.fraun_averaging.slow_trap_ramp(j)
if __name__ == "__main__":
my_obj = MyClass()
my_obj.demag_chip()
答案 1 :(得分:1)
错误很简单。
j
您使用参数self
调用此方法,但该方法不接受参数(def _slow_trap_ramp(self, j):
除了用于传递对象的参数)。
如果要传递参数,请重新定义接受参数的方法:
{{1}}