简体   繁体   中英

TypeError: super() argument 1 must be type, not int (Python)

I am trying to create a 'Group Chat' class that inherits some of its attributes from the main 'Chat' class. I am getting an error at the "super(chat_id, user1). init ()" line. I am unable to fix it!

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)   

Upon instantiating 'GroupMessage', I get an error!

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

TypeError: super() argument 1 must be type, not int

You should do instead of super(chat_id, user1).__init__() you should do:

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

or

Chats.__init__(self, chat_id, user1)

This last option is not recommended if exist change that your class hierarchy change in the future. I really don't like it for others motive but still worth it a mention.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM