基本上,我想運行連接功能,但我不斷收到CMD錯誤消息'類StraussBot沒有屬性'connectSock',但我可以明顯地看到它的確如此。我試過在這裏搜索,我找不到解決這個問題的任何解決方案。所以,如果你能幫助我找到爲什麼沒有找到'connectSock'函數,將會非常感激。Python類無法找到屬性
代碼:
import socket
from config import HOST, PORT, CHANNEL
# User Info
USER = "straussbot" # The bots username
PASS = "oauth:sj175lp884ji5c9las089sm9vvaklf" # The auth code
class StraussBot:
def __init__(self):
self.Ssock = socket.socket()
def connectSock(self):
self.Ssock.connect((HOST, PORT))
self.Ssock.send(str("Pass " + PASS + "\r\n").encode('UTF-8'))
self.Ssock.send(str("NICK " + USER + "\r\n").encode('UTF-8'))
self.Ssock.send(str("JOIN " + CHANNEL + "\r\n").encode('UTF-8'))
if __name__ == "__main__":
print "Starting the bot..."
while True:
straussbot = StraussBot
try:
straussbot.connectSock()
except Exception as e:
print e
'straussbot = StraussBot()',你失蹤parens。 –
在您的主要區塊中,您將'straussbot'設置爲_class_ StraussBot,而不是_instance_類。 – ajwood