I can't get past the error:
Traceback (most recent call last): File "C:\Users\Dom\Desktop\test\test.py", line 7, in <module> p = Pump.getPumps() TypeError: getPumps() missing 1 required positional argument: 'self'
I examined several tutorials but there doesn't seem to be anything different from my code. The only thing I can think of is that Python 3.3 requires different syntax.
class Pump: def __init__(self): print("init") # never prints def getPumps(self): # Open database connection # some stuff here that never gets executed because of error pass # dummy code p = Pump.getPumps() print(p)
If I understand correctly, self
is passed to the constructor and methods automatically. What am I doing wrong here?
The Python "TypeError: __init__() missing 1 required positional argument" occurs when we forget to provide a required argument when instantiating a class. To solve the error, specify the argument when instantiating the class or set a default value for the argument.
The Python "TypeError: missing 2 required positional arguments" occurs when we forget to provide 2 required arguments when calling a function or method. To solve the error, specify the arguments when calling the function or set default values for the arguments.
Using Positional Arguments Followed by Keyword Arguments One method is to simply do what the error states and specify all positional arguments before our keyword arguments! Here, we use the positional argument 1 followed by the keyword argument num2=2 which fixes the error.
“Positional argument” means data that we pass to a function, and the parentheses () after the function name are for required arguments. Every function within a class must have “self” as an argument. “self” represents the data stored in an object belonging to a class.
You need to instantiate a class instance here.
Use
p = Pump() p.getPumps()
Small example -
>>> class TestClass: def __init__(self): print("in init") def testFunc(self): print("in Test Func") >>> testInstance = TestClass() in init >>> testInstance.testFunc() in Test Func
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With