A
You're trying to turn to class method. Chat It's like static, though it's not. You need to hand over your class object in any way. Chat in class Human and Robot♪ For example:class Human:
def __init__(self, chat, name):
self.name = name
self.chat = chat
def send(self, sms):
chat.communication(sms)
class Robot:
def init(self, chat, name):
self.name = name
self.chat = chat
def send(self, sms):
chat.communication(sms)
class Chat:
def init(self):
self.chat_history = ""
def connect_human(self, human: Human):
self.name_human = human.name
def connect_robot(self, robot: Robot):
self.name_robot = robot.name
def communication(self, sms):
self.chat_history += sms
if name == 'main':
# These "asserts" using only for self-checking and not necessary for auto-testing
chat_100 = Chat()
karl = Human(chat_100, "Karl")
bot = Robot(chat_100, "R2D2")
chat_100.connect_human(karl)
chat_100.connect_robot(bot)
karl.send("Hi! What's new?")
bot.send("Hello, human. Could we speak later about it?")
In such a case, it is pointless to transfer objects to chat. carl and botif no further work is planned.In general: In order to cause a non-statistical class method, in any case it is necessary
having this class object.I haven't checked or processed your code from the point of view of the correct OPS, that's the subject for a separate discussion. I can say, however, that you should think about, and better read, the relevant literature on the correct design of classes and their interfaces, because at this point, the reciprocal inclusion of classes Human and Chat There is concern about each other (as is the situation with regard to each other) Chat and Robot)Option 2:In a situation where the code main You can't change, you should do something like that: class Human:
def init(self, name):
self.name = name
def connect_to_chat(chat):
self.chat = chat
def send(self, sms):
chat.communication(sms)
class Robot:
def init(self, name):
self.name = name
def connect_to_chat(chat):
self.chat = chat
def send(self, sms):
chat.communication(sms)
class Chat:
def init(self):
self.chat_history = ""
def connect_human(self, human: Human):
self.name_human = human.name
human.connect_to_chat(self)
def connect_robot(self, robot: Robot):
self.name_robot = robot.name
robot.connect_to_chat(self)
def communication(self, sms):
self.chat_history += sms
if name == 'main':
# These "asserts" using only for self-checking and not necessary for auto-testing
chat_100 = Chat()
karl = Human("Karl")
bot = Robot("R2D2")
chat_100.connect_human(karl)
chat_100.connect_robot(bot)
karl.send("Hi! What's new?")
bot.send("Hello, human. Could we speak later about it?")