类型错误:缺少 1 个必需的位置参数:'self' [英] TypeError: Missing 1 required positional argument: 'self'

查看:61
本文介绍了类型错误:缺少 1 个必需的位置参数:'self'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法克服错误:

回溯(最近一次调用最后一次):文件C:\Users\Dom\Desktop\test\test.py",第 7 行,在 <module> 中.p = Pump.getPumps()类型错误:getPumps() 缺少 1 个必需的位置参数:'self'

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

 类泵:def __init__(self):print("init") # 从不打印def getPumps(self):# 打开数据库连接# 这里有些东西因为错误而永远不会被执行通过 # 虚拟代码p = Pump.getPumps()打印(页)

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

解决方案

您需要在这里实例化一个类实例.

使用

p = Pump()p.getPumps()

小例子 -

<预><代码>>>>类测试类:def __init__(self):打印(在初始化")def testFunc(self):打印(在测试功能中")>>>测试实例 = 测试类()在初始化>>>testInstance.testFunc()在测试功能

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?

解决方案

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

这篇关于类型错误:缺少 1 个必需的位置参数:'self'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆