How do I merge the views of the items of two dicts in python?
My use case is: I have two private dictionaries in a class. I want the API to treat them as one, and provide an items
method as such. The only way I know of is to combine them then provide a view on the two, but for large dictionaries this seems expensive. I'm thinking of sth like a.items() + b.items()
Note: I don't care about key clashes.
This is what I'd like to improve:
class A:
_priv1 = {'a': 1, 'b': 2}
_priv2 = {'c': 1, 'd': 2}
def items(self):
return {**self._priv1, **self._priv2}.items()