M
M
Max Payne2018-09-03 12:14:03
Python
Max Payne, 2018-09-03 12:14:03

Why doesn't deepcopy work?

command = tools.get_attr_recursive(scripts, command_class_name) 
# command.name по умолчанию пуст
# print(command.name)
# ""
self.commands[command_name] = copy.deepcopy(command)
self.commands[command_name].name = command_name
print(command.name, self.commands[command_name].name)
# command.name == self.commands[command_name].name
# True

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stanislav Pugachev, 2018-09-03
@YardalGedal

short topic like this

import copy


class Command(object):
    name = ""


cmd = Command
cmd2 = copy.deepcopy(Command)
assert cmd == cmd2

copying really does not give anything, because as it is written here copy and deepcopy are copying objects
in our case, there really are no instances
what you want to do is to create a class with similar behavior but a little different
there is an option to inherit and redefine the necessary parts,
or you can assemble the class manually by reusing guts from the original class
class Command(object):
    name = ""

cmd = Command
cmd3 = type('NewCommand', Command.__bases__, dict(Command.__dict__))
assert cmd == cmd3  # AssertionError

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question