TypeError: super() argument 1 must be type, not int (Python)
TypeError: super() argument 1 must be type, not int (Python)
我正在尝试创建一个 'Group Chat' class,它继承了主要 'Chat' class 的一些属性。我在 "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)
在实例化 'GroupMessage' 时,出现错误!
> Chat_group = GroupMessage(1, "Batool","AI_group","Text Message")
TypeError: super() 参数 1 必须是类型,而不是 int
你应该做而不是 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)
如果您的 class 层次结构将来会发生变化,则不建议使用最后一个选项。我真的不喜欢别人的动机,但仍然值得一提。
我正在尝试创建一个 'Group Chat' class,它继承了主要 'Chat' class 的一些属性。我在 "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)
在实例化 'GroupMessage' 时,出现错误!
> Chat_group = GroupMessage(1, "Batool","AI_group","Text Message")
TypeError: super() 参数 1 必须是类型,而不是 int
你应该做而不是 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)
如果您的 class 层次结构将来会发生变化,则不建议使用最后一个选项。我真的不喜欢别人的动机,但仍然值得一提。