you can use dict.copy()
to get a new (shallow!) copy of kwargs
, like this:
class A:
def __init__(self, **kwargs):
self.local_copy = kwargs.copy()
print(id(self.local_copy))
a2 = A(x=2)
a3 = A(x=3)
print(a2.local_copy)
print(a3.local_copy)
example output:
15067008
15031824
{'x': 2}
{'x': 3}
EDIT:
as @chepner pointed out in the comments, the copy
is unnecessary.
I assume the reason you get the same id
for the 2 different invocations is that kwargs is unreferenced once the function returns, so the object may be garbage collected, and its address is reused.
even without copy, I get the same results: (2 different IDs, and distinct A objects)