python - self - required positional argument [duplicate]

我怕爱的太早我们不能终老 提交于 2019-12-13 11:08:56

问题


here is my code:

my file which I start:

 from SQLhandler import SQLhandler 
 D = SQLhandler.loadProject(4711)

a part of my SQLhandler file:

class SQLhandler(object):
   db = pymysql.connect(... )

   def loadProject(self, project_id):
    #do some stuff

I want to use db in other functions, so I put it on the class level and added a "self" to loadProject. Now the second line in my start file throws an error:

"loadProject() missing 1 required positional argument: 'project_id'"

What's wrong with my code?


回答1:


Within your class definition you need to have a def __init__(self, ... params): function that tells how to initialize a new instance. Try including something along the lines of

def __init__(self, project_id):
    self.project_id = project_id


来源:https://stackoverflow.com/questions/53218211/python-self-required-positional-argument

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!