2017-08-27 109 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

相关问题