2017-08-27 75 views
0

我正嘗試創建一個「羣聊」類,它從主「聊天」類繼承了它的一些屬性。我在「超級(chat_id,user1)」錯誤處獲得。init()「行。我無法修復它!TypeError:super()參數1必須是類型,而不是int(Python)

class Chats(object): 

def __init__(self, chat_id, user1): 
    self.id = chat_id 
    self.initiator = user1 
    self.messages = {} #key is current date/time; value is a list of messages 


class GroupMessage(Chats): 

def __init__(self, chat_id, user1, group_name, message): 
    super(chat_id, user1).__init__() 
    self.group = group 
    self.messages[datetime.datetime.now()] = self.messages[datetime.datetime.now()].append(message) 

在實例化'GroupMessage'時,出現錯誤!

> Chat_group = GroupMessage(1, "Batool","AI_group","Text Message") 

類型錯誤:超()參數1必須是類型,不是int

+0

好吧,正如它所說的,你需要傳遞一個類型作爲第一個參數,而不是一個整數。你讀過[文檔](https://docs.python.org/3/library/functions.html#super)嗎? – kindall

+2

這不是如何完成的......使用:super().__ init __(chat_id,user1)改變你的調用''super'' – alfasin

+0

@alfasin,我最初是這樣做的,但得到這個錯誤:TypeError:super()至少需要1個參數(給出0)。 – Batool

回答

3

你應該做的,而不是super(chat_id, user1).__init__()你應該做的:

super().__init__(chat_id, user1) # Work in Python 3.6 
super(GroupMessage, self).__init__(chat_id, user1) # Work in Python 2.7 

Chats.__init__(self, chat_id, user1) 

這如果存在會改變您的類層次結構在將來更改的情況,則不建議使用最後一個選項我真的不喜歡它爲別人動機,但仍值得一提。

+0

第二個與我的2.7工作! :) – Batool

相關問題