Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to "correctly" copy a types.SimpleNamespace object?

I was expecting to be able to do something like:

a = SimpleNamespace(x='test')
b = a.copy()

or maybe:

b = SimpleNamespace(a)

My current solution, which seems to work fine is

b = SimpleNamespace(**a.__dict__)

But it looks somewhat hacky. Is there a more "correct" way?

I don't need a deep copy.

like image 763
André C. Andersen Avatar asked Jun 19 '17 11:06

André C. Andersen


1 Answers

I wanted to use do a deepcopy of a SimpleNamespace. A simple and easy to read way is to use the copy module.

new_namespace = copy.copy(namespace)

or

new_namespace = copy.deepcopy(namespace)

depending on what you need.

like image 151
stochastic_random Avatar answered Oct 20 '22 10:10

stochastic_random