相关疑难解决方法(0)

TypeError:缺少1个必需的位置参数:'self'

我是python的新手,已经撞墙了.我遵循了几个教程,但无法通过错误:

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'
Run Code Online (Sandbox Code Playgroud)

我检查了几个教程,但似乎与我的代码没有任何不同.我唯一能想到的是python 3.3需要不同的语法.

主要内容:

# test script

from lib.pump import Pump

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

p = Pump.getPumps()

print (p)
Run Code Online (Sandbox Code Playgroud)

泵类:

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
Run Code Online (Sandbox Code Playgroud)

如果我理解正确,"self"会自动传递给构造函数和方法.我在这做错了什么?

我使用的是Windows 8和python …

python python-3.x

168
推荐指数
6
解决办法
58万
查看次数

Python:按实例对象调用方法:“缺少 1 个必需的位置参数:‘self’”

我是 Python 的新手。我写了两个类,第二个类有第一个类的实例作为成员变量。现在我想通过类一中的实例调用类 2 的方法。我找不到答案。像这样的东西:

class Class1:
    def uselessmethod(self):
        pass

class Class2:
    def __init__(self):
        self.c = Class1()

    def call_uselessmethod(self):
        self.c.uselessmethod()

k = Class2
k.call_uselessmethod() # Error!
Run Code Online (Sandbox Code Playgroud)

给出以下错误:

k.call_uselessmethod() #Error
TypeError: call_uselessmethod() missing 1 required positional argument: 'self'
Run Code Online (Sandbox Code Playgroud)

知道这里发生了什么吗?提前致谢。

python methods class instance

5
推荐指数
1
解决办法
5545
查看次数

Python unittest继承setUp覆盖

import unittest

class A(unittest.TestCase):
    def setUp(self):
        print "Hi it's you",self._testMethodName

    def test_one(self):
        self.assertEqual(5,5)

    def tearDown(self):
        print "Bye it's you", self._testMethodName

class B(A,unittest.TestCase): 

    def setUp(self):
        print "Hi it's me", self._testMethodName

    def test_two(self):
        self.assertNotEqual(5,5)


unittest.main()
Run Code Online (Sandbox Code Playgroud)

输出 :

Hi it's you test_one
Bye it's you test_one
.Hi it's me test_one
Bye it's you test_one
.Hi it's me test_two
FBye it's you test_two

======================================================================
FAIL: test_two (__main__.B)
----------------------------------------------------------------------

Traceback (most recent call last):
  File "try_test_generators.py", line 19, in test_two
    self.assertNotEqual(5,5)
AssertionError: 5 == 5 …
Run Code Online (Sandbox Code Playgroud)

python python-unittest

3
推荐指数
1
解决办法
3714
查看次数

标签 统计

python ×3

class ×1

instance ×1

methods ×1

python-3.x ×1

python-unittest ×1