-3

Why do i get the error missing required positional argument self when i call inp() on the object.

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()
Martin Backasch
  • 1,829
  • 3
  • 20
  • 30
  • Possible duplicate of [TypeError: Missing 1 required positional argument: 'self'](https://stackoverflow.com/questions/17534345/typeerror-missing-1-required-positional-argument-self) – Daniel W Strimpel Apr 02 '18 at 20:39
  • 3
    What's `q`? Why use `x` in 2 different contexts? What's `no`, `at`, `bt` in `__init__`? Please provide a **[mcve]**. – jpp Apr 02 '18 at 20:47

1 Answers1

1

At first I have to admit, I did not wrote that much python code in my live, so please don't be so censorious, if I used wrong terminology.

But as far as I know you are try to call the inp() method as a static method, because the x in

for x in process:
    x.inp()

isn't a new instance of the type Process. It is just the type Process, because you add the type Process to your array and not a new instance.

for i in range(x):
    process.append(Process)

So let us say the user enters 5 as the number of processes. Now your for loop will run five times and add items to your process array. This array will now look like:

process = [[0:Process] [1:Process] [2:Process] [3:Process] [4:Process]]

which leads that you call five times Process.inp() as a static method in

for x in process:
    x.inp()

instead of an object method like

p1.inp()
p2.inp()
...
p5.inp()

To fix this issue you could change your code to

for i in range(x):
    q = Process()
    process.append(q)
    q.no=i

and call later on

for x in process:
    x.inp()

But this will leads to another error, since you have defined _init__(self) and try to set three properties you did not defined in the __init__signature.

So you could remove def __init__ or add these parameter:

def __init__(self, no, at, bt):
    self.no=no
    self.at=at
    self.bt=bt

and create a new object with

q = Process(1, 2, 3)

At least I guess there is a little typo in your inp() method. Did you really want to set ar? I guess you mean self.at.

Martin Backasch
  • 1,829
  • 3
  • 20
  • 30
  • Yes,it's actually self.at. And thanks fixed it.But why x isn't the object i want to call inp() upon. – Prajwal Pathak Apr 03 '18 at 08:55
  • @PrajwalPathak: _But why x isn't the object i want to call inp() upon_. I try to express myself a little bit clearer. The `x` you are calling within your `for x in process:` is only an object of the __Type__ `Process` not a __new instance__ of this type. So if we assume the `x` within your `for i in range(x): process.append(Process)` is _5_. You will add five times an object of the type `Process` to your `process` array and not five separate objects _`p1, p2, ...`_. So I presumed to think you want to call `x` as an instance instead of the type. I hope this clarified it a little bit better. – Martin Backasch Apr 03 '18 at 09:30