为什么我在调用时会错过错误所需的位置参数self 对象上的inp()。
process=[]
class Process:
def __init__(self):
self.no=no
self.at=at
self.bt=bt
def inp(self):
ar=int(input('Enter arrival time'))
bt=int(input('Enter burst time'))
x=int(input('Enter the no. of processes'))
for i in range(x):
process.append(Process)
q.no=i
for x in process:
x.inp()
答案 0 :(得分:1)
起初我不得不承认,我没有在我的直播中写过那么多python代码,所以如果我使用了错误的术语,请不要那么苛刻。
但据我所知,您尝试将inp()
方法称为静态方法,因为
x
for x in process:
x.inp()
不是Process
类型的新实例。它只是类型Process
,因为您将类型Process
添加到数组而不是新实例。
for i in range(x):
process.append(Process)
因此,我们假设用户输入5
作为进程数。现在你的for循环将运行五次,并将项目添加到process
数组中。此数组现在看起来像:
process = [[0:Process] [1:Process] [2:Process] [3:Process] [4:Process]]
导致您在中将Process.inp()
作为静态方法调用五次
for x in process:
x.inp()
而不是像
这样的对象方法p1.inp()
p2.inp()
...
p5.inp()
要解决此问题,您可以将代码更改为
for i in range(x):
q = Process()
process.append(q)
q.no=i
稍后致电
for x in process:
x.inp()
但这会导致另一个错误,因为您已定义_init__(self)
并尝试设置未在__init__
签名中定义的三个属性。
因此,您可以删除def __init__
或添加以下参数:
def __init__(self, no, at, bt):
self.no=no
self.at=at
self.bt=bt
并使用
创建一个新对象q = Process(1, 2, 3)
至少我猜你的inp()
方法中有一点错字。你真的想设置ar
吗?我想你的意思是self.at
。