Updating a property used in dict in a class

I am trying to achieve to dynamically update some parameters in object. Let’s have a property that I use in some dict ->

class Test():
    def __init__(self):
        self._id = None
        self.some_dict = {"id": self.id}

    @property
    def id(self):
        return self._id

    @id.setter
    def id(self, new_id: int):
        self._id = new_id

tst = Test()
tst.id = 112
print(tst.id) # 112
print(tst.some_dict) # {'id': None}

The desired result is to have:

print(tst.some_dict) # {'id': 112}

How to update the dictionary after some parameter (id) as a property will be updated?

Edit: Sure thing I can do:

@id.setter
def id(self, new_id: int):
    self._id = new_id
    self._some_dict.update({"id": self._id})

But for a lot of parameters it is kind of exhausting to write, is there any automatic way?

Answer

You could access the __dict__ attribute of your instance and return a sanitized version of this dict.

Example: (should work as is)

class A:
    _prohibited_keys = ["instance", "variables", "you", "dont", "want", "to", "show"]

    def __init__(self):
        self._id = None
    @property
    def id(self):
        return self._id

    @id.setter
    def id(self, value: int):
        self._id = value

    @property
    def dict(self):
        return {self._private_to_pub_key(k): v for k, v in self.__dict__.items()
                if k not in self._prohibited_keys and not k.startswith('__')}

    def _private_to_pub_key(self, k):
        return k[1:] if k.startswith('_') else k




a = A()
print(a.dict)
>>> {'id': None}