TypeError: ไม่มี 1 อาร์กิวเมนต์ที่ต้องการตำแหน่ง: 'ตัวเอง'


217

ฉันยังใหม่กับงูหลามและเข้าชนกำแพง ฉันติดตามหลายบทเรียน แต่ไม่สามารถแก้ไขข้อผิดพลาดได้:

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'

ฉันตรวจสอบบทเรียนหลายอย่าง แต่ดูเหมือนจะไม่แตกต่างจากรหัสของฉัน สิ่งเดียวที่ฉันนึกได้ก็คือ python 3.3 ต้องใช้ไวยากรณ์ต่างกัน

หลัก scipt:

# test script

from lib.pump import Pump

print ("THIS IS A TEST OF PYTHON") # this prints

p = Pump.getPumps()

print (p)

คลาสปั๊ม:

import pymysql

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

ถ้าฉันเข้าใจถูกต้อง "ตัวเอง" จะถูกส่งไปยังตัวสร้างและวิธีการโดยอัตโนมัติ ฉันทำอะไรผิดที่นี่

ฉันใช้ windows 8 กับ python 3.3.2

คำตอบ:


281

คุณต้องยกตัวอย่างคลาสที่นี่

ใช้

p = Pump()
p.getPumps()

ตัวอย่างเล็ก ๆ -

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


>>> testInstance = TestClass()
in init
>>> testInstance.testFunc()
in Test Func

1
พยายามมาก่อน แต่ไม่มี "()" ใหม่ใน python 3.x ไหม?
DominicM

1
@DominicM: ไม่เลยนั่นอยู่ที่นั่นเสมอ
Sukrit Kalra

1
ใช่ฉันมองย้อนกลับไปที่แบบฝึกหัดที่ฉันตามมาสมองของฉันจะต้องใส่เครื่องหมายวงเล็บออกไป :)
DominicM

4
ชื่อคลาสควรเป็นตัวพิมพ์ใหญ่เช่น 'testClass' ควรเป็น 'TestClass'
eggonlegs

1
ขอบคุณปัญหาที่ฉันได้รับคือวงเล็บที่หายไปในวงเล็บ ตัวอย่างของคุณชัดเจนมาก
Paul Watson

57

คุณต้องเริ่มต้นก่อน:

p = Pump().getPumps()

14
ความเรียบง่ายมักไม่ได้รับการจัดอันดับ
theeastcoastwest

14
การทำเช่นนี้จะทำให้ p เท่ากับเมธอด getPumps () ในขณะที่สิ่งนี้จะเรียกใช้ p จะไม่สามารถ 'ใช้งานได้' เป็นตัวแปรสำหรับคลาส Pump () นี่ไม่ใช่วิธีปฏิบัติที่ดีเช่นเดียวกับการสร้างตัวแปรที่ไร้ประโยชน์ หากเป้าหมายเดียวคือการเรียกใช้ฟังก์ชัน getPumps มันจะทำงานได้แค่เรียกใช้ Pump (). getPumps () แทนที่จะสร้างตัวแปรสำหรับฟังก์ชัน
Ashmoreinc

7

ใช้งานได้ง่ายกว่าโซลูชันอื่น ๆ ที่ฉันเห็นที่นี่:

Pump().getPumps()

นี่เป็นสิ่งที่ดีถ้าคุณไม่จำเป็นต้องใช้อินสแตนซ์ของคลาสอีกครั้ง ทดสอบกับ Python 3.7.3


2

นอกจากนี้คุณยังสามารถรับข้อผิดพลาดนี้ได้โดยทำตามคำแนะนำของ PyCharm ก่อนกำหนดเพื่อใส่คำอธิบายประกอบเมธอด @staticmethod ลบคำอธิบายประกอบ


1

'ตัวเอง'คำหลักในหลามจะคล้ายคลึงกับ'นี้'คำหลักใน c ++ / java / C #

ในหลาม 2 (yes python does compilation internally)มันจะทำโดยปริยายคอมไพเลอร์ เป็นเพียงแค่ใน python 3 คุณต้องพูดถึงมันexplicitlyในฟังก์ชั่นการสร้างและสมาชิก ตัวอย่าง:

 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()
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.