__dict__

class Target(object):

    def __init__(self):
        self.a = 1
        self.b = 2


t = Target()
t.__dict__
# {'a': 1, 'b': 2}

reassigning leads to replacement.

t.__dict__ = { 'a': 3 }
t.__dict__
# {'a': 3}

t.b
# AttributeError: 'Target' object has no attribute 'b'

update changes some items only.

t.__dict__ = { 'a': 1, 'b': 2 }
t.__dict__
# {'a': 1, 'b': 2}

t.__dict__.update({'a':10, 'd': 11})
# {'a': 10, 'b': 2, 'd': 11}
t.__dict__
t.b
# 2

Last updated

Was this helpful?