I have taken this code from a larger program, and stripped out almost everything so as to illustrate the core of the problem.
The program is used to create multiple "rooms" that initially have the same attributes, but are later changed such that they are two separate objects.
To create two completely independent Room objects, I have tried to use copy.deepcopy(), but this gives: "TypeError: cannot pickle '_curses.window' object"
import curses
from curses import wrapper
import copy
def main(stdscr):
bg = Background(100, 237)
test_room = Room(10, 15, bg)
new_room = copy.deepcopy(test_room)
print("Test deepcopy: ", new_room.height, new_room.width) ### See if attributes copied successfully
class Background():
def __init__(self, height, width):
self.height = height
self.width = width
self.pad = curses.newpad(self.height, self.width)
class Room():
def __init__(self, height, width, background):
self.height = height
self.width = width
self.bg = background.pad
self.pad = background.pad.subpad(self.height, self.width)
### def __deepcopy__(self, memo)
###return self
while True:
curses.wrapper(main)
I see that the problem is with the self.bg and self.pad lines in the Room class; if these are commented out it works in all other respects, but obviously I need these lines for it to do what I need it to do. I tried defining my own deepcopy method (see commented out in Room class below), but I don't really understand how to use it properly and it doesn't appear to solve the problem.
I get that I am probably making multiple mistakes. What am I doing wrong, and what would be a better solution?