class X(object): ab = 5 x = X() name = 'a' x.__dict__[name + 'b'] = 42 assert x.__dict__.keys()[0] is not 'ab' x.__dict__[0] = 0 def f(): assert x.ab == 42 f() f()