Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python “如何修复”;TypeError:printSong()缺少1个必需的位置参数:';自我'&引用;_Python - Fatal编程技术网

Python “如何修复”;TypeError:printSong()缺少1个必需的位置参数:';自我'&引用;

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):

调用printSong()方法时,会收到一条错误消息:

TypeError:printSong()缺少1个必需的位置参数:“self”

所以。。我应该在parentesis中加入什么论据

我可以从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'