I am new here and not sure how I can suggest this properly. When using nested SimpleNamespaces, a making a copy by using new_NS=SimpleNamespace(**namespace.__dict__.copy()) only copies the highest level namespace. This is expected in python as shallow copies are preferred. But, a nested deep copy function would be nice, and will allow easier use. I suggest a simple def my_namespace_copy(namespace): '''Recursively deep copies nested namespaces''' new_NS=SimpleNamespace(**namespace.__dict__.copy()) for i in new_NS.__dict__.keys(): if(type(new_NS.__dict__[i]) == types.SimpleNamespace): new_NS.__setattr__(i, my_namespace_copy(new_NS.__getattribute__(i))) return new_NS I am not sure of the exact implementation of the class and guess this would need some appropriate modifications. I suggest this be added at SimpleNameSpace.__copy__ or at SimpleNameSpace.__deepcopy__
> When using nested SimpleNamespaces, a making a copy by using > > new_NS=SimpleNamespace(**namespace.__dict__.copy()) In general, you shouldn't call or directly access dunder attributes. There are exceptions, but generally they're for Python's use only. For example, the public interface for getting __dict__ is to call vars(namespace). But there's no need to do this by hand. Use the copy module instead. copy.copy(namespace) # copy one level only copy.deepcopy(namespace) # copy all the way down seem to work for me. Does this solve your problem? If so, we'll close this issue.
Hmm. What problems are you seeing with deep copies? copy.deepcopy() should work since SimpleNamespace is picklable. [1][2] I don't have any problems: >>> import types, copy >>> ns = types.SimpleNamespace(x=1, y=2) >>> copied = copy.deepcopy(ns) >>> copied namespace(x=1, y=2) >>> ns = types.SimpleNamespace(x=types.SimpleNamespace(a=1), y=types.SimpleNamespace(b=2)) >>> copied = copy.deepcopy(ns) >>> copied namespace(x=namespace(a=1), y=namespace(b=2)) >>> ns.x is copied.x False [1] issue #15022 [2] https://docs.python.org/3/library/copy.html