M
M
Max Payne2018-09-07 16:43:37
Python
Max Payne, 2018-09-07 16:43:37

Why does data remain after creating a new object?

I am using Tornado version 5 and Python 3.6
. There is a Tornado handler that looks something like this:

class Cb
class Cb(TornadoBots):
    bot = None
    answer = None

    data = dict()

    @gen.coroutine
    def post(self, *args, **kwargs):
        bot_name, key = args
...
                else:
                    self.__make_answer()
                    try:
                        self.__send_answer()
                        self.write('ok')
                    except Exception as e:
                        print(e)
...
    def __make_answer(self):
        msg = some_data
        self.answer = answer.Vk(self.bot, msg)

        if data and self.__check_new_message():
            self.answer.handle()
        else:
           ...
    def __send_answer(self):
        try:
            if self.answer and self.answer.data:
                return self.answer.send()
        except Exception as e:
            print(e)


And the answer module, which contains the classes Answer, Vk (Answer) and so on.
All changes to the data attribute of the Vk(Answer) class occur in only one method
__prepare_answer
def __prepare_answer(self):
        self.data.update({'peer_id': self.message.peer_id})

        self.command = self.command(...)
        self.command.run()

        if self.command.data_type == 'action':
            return

        if ...:
            self.data.update({self.command.data_type: self.command.data})
        else:
            self.data.update(self.command.data)

        if ...:
            self.data['message'] = tools.get_message('MAKE_ANSWER_ADD_VIP_EMOJI', {'my': self})

        if ...:
            self.data['forward_messages'] = ...

        if ...:
            self.data['keyboard'] = ...

        if ...:
            self.data['message'] += ...

After processing the command, the old data, which after the execution of the previous command remained in answer.data, miraculously gets into the new object.
Simple example:
After the first execution in self.answer.data (of class Cb) I have
{"attachment": "photoXXX_XXX,photoYYY_YYY"}
# потому что в методе __prepare_answer self.command.data = "photoXXX_XXX,photoYYY_YYY"

And after the second execution in self.answer.data (class Cb) I have
{"message": "Some Text", "attachment": "photoXXX_XXX,photoYYY_YYY"}
# хотя self.command.data = "Some Text"

How can this data get there, because a completely new answer.Vk object is created in Cb?
The problem is solved simply: in the first line of __prepare_answer instead of .update() use =, but I want to eliminate the problem entirely.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question