Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: Missing 1 required positional argument: 'self'

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?

like image 857
DominicM Avatar asked Jul 08 '13 19:07

DominicM


People also ask

How do you fix a missing one required positional argument?

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.

How do you fix missing positional arguments in python?

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.

How do you fix a positional argument error?

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.

Is self a positional argument in Python?

“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.


1 Answers

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 
like image 81
Sukrit Kalra Avatar answered Oct 05 '22 08:10

Sukrit Kalra