0

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?

JD Allen
  • 1
  • 1
  • I don't know how accustomed you're to Design Patterns, but "The program is used to create multiple "rooms" that initially have the same attributes, but are later changed..." gives me the impression you could use maybe Factories or Builders to create instances of "rooms" using the same attributes every time you want at run time. With that option, you don't have to deepcopy objects. – ccolin Jan 25 '23 at 05:13
  • Other thing you could try is making those fields you found are un-pickleable and make them pickleable, maybe this could help you: https://stackoverflow.com/questions/70128335/what-is-the-proper-way-to-make-an-object-with-unpickable-fields-pickable – ccolin Jan 25 '23 at 05:14

0 Answers0