Skip to content Skip to sidebar Skip to footer

Error:__init__() Missing 1 Required Positional Argument: 'rec'

I am new to python. I am trying to do microphone file that ought to detect, listen, record and write the .wav files. However, it is giving me an error while I am trying to run the

Solution 1:

You declare your __init__ as staticmethod and so there is no self (or in your case rec) argument passed into your constructor when you create an object of your class.

k = Recorder() <--- will pass a reference, typical named self, into your __init__

Please take a look at

  1. this
  2. or this
  3. and this

when you want to use a static constructor.

Solution 2:

There many problems in your code and one of them is that, the first argument of member functions must be self (like the this pointer). This is because, python's philosophy is,

Explicit is better than implicit

In general, a python class looks like this:

classMyClass:def__init__(self, val):
        self.val = val

    defgetVal(self):
        returnself.val

    defsetVal(self, val):
        self.val = val

obj = MyClass(5)
print(obj.getVal()) # prints 5
obj.setVal(4)
print(obj.getVal()) # prints 4

When you refactor your code to the above grammar, your code would start working. Also, do pick up some book that gives you an introduction to python in a systematic manner :)

Post a Comment for "Error:__init__() Missing 1 Required Positional Argument: 'rec'"