I have initialized a class with one positional argument (defaults to an empty dict) that gets updated inside the __init__ function with the kwargs items.
The problem is when I make multiple instances using kwargs
argument, the subsequent calls gets the items from previous calls dict, but, given that every call is for a fresh instance, I would expect that data and kwargs
arguments had a limited scope to the init method, here are some examples:
class Test:
def __init__(self, data: dict = {}, **kwargs):
data.update(kwargs)
self.data = data
d1 = {"A": 1, "B": 2}
d2 = {"A": 1, "C": 2}
t1 = Test(**d1)
print(t1.data)
# printout -> {'A': 1, 'B': 2}
t2 = Test(**d2)
print(t1.data)
# printout -> {'A': 1, 'B': 2, 'C': 2}
# expected -> {'A': 1, 'A': 2}
print(t2.data)
# printout -> {'A': 1, 'B': 2, 'C': 2}
# expected -> {'A': 1, 'B': 2}