[New-bugs-announce] [issue31322] SimpleNamespace deep copy

Pritish Patil report at bugs.python.org
Fri Sep 1 10:51:16 EDT 2017


New submission from Pritish Patil:

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__

----------
assignee: docs at python
components: Documentation, Library (Lib)
messages: 301112
nosy: Pritish Patil, docs at python
priority: normal
severity: normal
status: open
title: SimpleNamespace deep copy
type: enhancement
versions: Python 3.5, Python 3.6, Python 3.7

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


More information about the New-bugs-announce mailing list