[issue31322] SimpleNamespace deep copy

Steven D'Aprano report at bugs.python.org
Fri Sep 1 11:06:19 EDT 2017


Steven D'Aprano added the comment:

> 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.

----------
nosy: +steven.daprano

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue31322>
_______________________________________


More information about the Python-bugs-list mailing list