TypeError: Missing 1 required positional argument: 'self'

后端 未结 6 822
借酒劲吻你
借酒劲吻你 2020-11-22 02:18

I am new to python and have hit a wall. I followed several tutorials but cant get past the error:

Traceback (most recent call last):
  File \"C:\\Users\\Dom\         


        
6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-22 03:03

    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.

提交回复
热议问题