Python “如何修复”;TypeError:printSong()缺少1个必需的位置参数:';自我'&引用;
调用printSong()方法时,会收到一条错误消息: TypeError:printSong()缺少1个必需的位置参数:“self” 所以。。我应该在parentesis中加入什么论据 我可以从printSong()方法中删除“self”。。。但是这种方法不起作用Python “如何修复”;TypeError:printSong()缺少1个必需的位置参数:';自我'&引用;,python,Python,调用printSong()方法时,会收到一条错误消息: TypeError:printSong()缺少1个必需的位置参数:“self” 所以。。我应该在parentesis中加入什么论据 我可以从printSong()方法中删除“self”。。。但是这种方法不起作用 class song: def __init__(self): self.title = "" self.artist = "" def printSong(self):
class song:
def __init__(self):
self.title = ""
self.artist = ""
def printSong(self):
return "The song " + self.title + " is song by " + self.artist
songs = {}
songs[1] = song
songs[1].title = "Gloria"
songs[1].artist = "U2"
@app.route("/")
def homepage():
return songs[1].printSong()
我想在主页上列出第一首歌“Gloria”的信息,
相反,我会收到一条错误消息:
内部服务器错误
服务器遇到内部错误,无法完成您的请求。服务器过载或应用程序出错。您尚未实例化该类:
songs[1]=song()#这里需要括号
否则,song
没有self
属性,因为从未调用过\uuuu init\uuuu
:
s=song
#s是类歌曲,不是实例
s
s、 printSong()
#引发错误,因为printSong是一个实例方法,但因为
#您并不是从类的实例调用它
#此方法不可用
s=宋()
s
#现在s是一个实例,您可以调用实例方法,因为
#他们有机会了解自己
将行更改为歌曲[1]=歌曲()
是的。。完全正确我还在学习如何在Python中使用OOP:-)你们帮了我很多忙!谢谢
class song:
def __init__(self):
self.title = ""
self.artist = ""
def printSong(self):
return "The song " + self.title + " is song by " + self.artist
songs = {}
songs[1] = song()
songs[1].title = "Gloria"
songs[1].artist = "U2"
songs[1].printSong()
'The song Gloria is song by U2'