TypeError: brak 1 wymaganego argumentu pozycyjnego: 'self'

Nie mogę ominąć błędu:

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'

Przejrzałem kilka samouczków, ale wydaje się, że nie ma nic innego niż mój kod. Jedyne, co przychodzi mi do głowy, to to, że python 3.3 wymaga innej składni.

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)

Jeśli dobrze rozumiem, {[2] } jest przekazywana do konstruktora i metod automatycznie. Co ja tu robię źle?

Author: wjandrea, 2013-07-08

6 answers

Musisz utworzyć instancję klasy tutaj.

Użyj

p = Pump()
p.getPumps()

Mały przykład-

>>> class TestClass:
        def __init__(self):
            print("in init")
        def testFunc(self):
            print("in Test Func")


>>> testInstance = TestClass()
in init
>>> testInstance.testFunc()
in Test Func
 340
Author: Sukrit Kalra,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-03-06 11:34:15

Najpierw musisz ją zainicjować:

p = Pump().getPumps()
 71
Author: JBernardo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-07-08 19:23:10

Działa i jest prostsze niż każde inne rozwiązanie, które tu widzę:

Pump().getPumps()

Jest to świetne rozwiązanie, jeśli nie musisz ponownie używać instancji klasy. Testowane na Pythonie 3.7.3.

 7
Author: Jay D.,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-05-18 09:50:49

Słowo kluczowe self w Pythonie jest analogiczne do słowa kluczowego this W C++ / Java / C#.

W Pythonie 2 jest to wykonywane domyślnie przez kompilator (tak Python wykonuje kompilację wewnętrznie). Chodzi o to, że w Pythonie 3 musisz o tym wspomnieć jawnie w konstruktorze i funkcjach członkowskich. przykład:

 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()
 3
Author: Tahir77667,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-07-30 19:15:32

Można również uzyskać ten błąd, przedwcześnie stosując się do Rady PyCharm, aby dodać adnotację do metody @ staticmethod. Usuń adnotację.

 2
Author: gherson,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-12-03 15:58:11

Możesz wywołać metodę jak pump.getPumps(). Poprzez dodanie @classmethod dekoratora do metody. Metoda klasy otrzymuje klasę jako pierwszy argument, podobnie jak metoda instancji otrzymuje instancję.

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

Więc po prostu zadzwoń Pump.getPumps() .

W języku java jest ona określana jako metoda static.

 0
Author: Atom,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-09-04 09:23:30