360

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?

mkrieger1
  • 14,486
  • 4
  • 43
  • 54
DominicM
  • 5,642
  • 13
  • 37
  • 60

6 Answers6

478

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
gl393
  • 301
  • 3
  • 13
Sukrit Kalra
  • 30,727
  • 7
  • 64
  • 70
83

You need to initialize it first:

p = Pump().getPumps()
JBernardo
  • 30,604
  • 10
  • 86
  • 109
17

Works and is simpler than every other solution I see here :

Pump().getPumps()

This is great if you don't need to reuse a class instance. Tested on Python 3.7.3.

Jay D.
  • 1,027
  • 3
  • 12
  • 24
  • This is practically a duplicate of [JBernardo's answer](https://stackoverflow.com/a/17534365/4518341). Note that he also doesn't save the instance, unless `getPumps()` returns `self`, which would be bizarre. – wjandrea Dec 31 '20 at 04:18
7

The self keyword in Python is analogous to this keyword in C++ / Java / C#.

In Python 2 it is done implicitly by the compiler (yes Python does compilation internally). It's just that in Python 3 you need to mention it explicitly in the constructor and member functions. example:

class Pump():
    # member variable
    # account_holder
    # balance_amount

    # constructor
    def __init__(self,ah,bal):
        self.account_holder = ah
        self.balance_amount = bal

    def getPumps(self):
        print("The details of your account are:"+self.account_number + self.balance_amount)

# object = class(*passing values to constructor*)
p = Pump("Tahir",12000)
p.getPumps()
Ghost Ops
  • 1,664
  • 2
  • 10
  • 22
Tahir77667
  • 1,878
  • 15
  • 13
  • 2
    It's not a keyword, just a convention. – wjandrea Jul 30 '20 at 19:17
  • 1
    What do you mean by *"In Python 2 it is done implicitly by the compiler"*? AFAIK Python 2 never set `self` implicitly. – wjandrea Jul 30 '20 at 19:17
  • This is not valid Python code. What are you trying to demonstrate exactly? To start, Python comments use `#`, not `//`; you don't need to declare member attributes at the class-level; and those pipes seem to be out-of-place. – wjandrea Dec 31 '20 at 04:16
  • *"In Python 2 it is done implicitly by the compiler"* citation needed. – Sören Apr 22 '22 at 20:55
4

You can also get this error by prematurely taking PyCharm's advice to annotate a method @staticmethod. Remove the annotation.

gherson
  • 133
  • 1
  • 8
3

You can call the method like pump.getPumps(). By adding @classmethod decorator on the method. A class method receives the class as the implicit first argument, just like an instance method receives the instance.

class Pump:

def __init__(self):
    print ("init") # never prints

@classmethod
def getPumps(cls):
            # Open database connection
            # some stuff here that never gets executed because of error

So, simply call Pump.getPumps() .

In java, it is termed as static method.

Atom
  • 132
  • 3
  • 11