TypeError:super()参数1必须是type,而不是int(Python)

Bat*_*ool 0 python inheritance typeerror superclass

我正在尝试创建一个"群聊"类,它继承了主要"聊天"类中的一些属性.我在"super(chat_id,user1).init()"行中收到错误.我无法解决它!

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)   
Run Code Online (Sandbox Code Playgroud)

在实例化'GroupMessage'时,我收到错误!

>  Chat_group = GroupMessage(1, "Batool","AI_group","Text Message")
Run Code Online (Sandbox Code Playgroud)

TypeError:super()参数1必须是type,而不是int

gsi*_*ank 7

你应该做而不是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
Run Code Online (Sandbox Code Playgroud)

要么

Chats.__init__(self, chat_id, user1)
Run Code Online (Sandbox Code Playgroud)

如果存在更改您的类层次结构将来发生更改,则不建议使用此最后一个选项.我真的不喜欢别人的动机,但仍然值得一提.