简体   繁体   English

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

[英]Missing 1 required positional argument: 'self' missing

I have found a couple of Stackoverflow posts where people have had the same issue but cannot seem to implement the suggested fixes in a satisfactory mannor for python to compile my script. 我发现了一些Stackoverflow帖子,那里的人们遇到了同样的问题,但是似乎无法以令人满意的方式实施建议的修复程序,以便python编译我的脚本。

I understand that it wants self to be passed into database but cannot understand where I am going to get this from as self from my main.py contains the wrong context. 我知道它希望将self传递到数据库中,但无法理解我将从哪里获取它,因为main.py中的self包含错误的上下文。

I have my main class in which I want to call the database class and execute a function from within. 我有主类,我想在其中调用数据库类并从内部执行一个函数。

main.py main.py

class Main:
    def __init__(self):
        self.db = Database
        self.db.createConnection()

database.py database.py

class Database:
    def __init__(self):
            #some setup here

    def createConnection(self):
            #Create connection code here

When I run this, I am presented with the following error. 运行此命令时,出现以下错误。

TypeError: createConnection() missing 1 required positional argument: 'self'

You want to assign an instance of Database to self.db , not assign the Database class itself. 您想将Database的实例分配给self.db ,而不要分配Database类本身。 Add parentheses to call Database and instantiate it: 添加括号以调用Database并实例化它:

self.db = Database()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM