Skip to content

Instantly share code, notes, and snippets.

@LifeMoroz
Created May 27, 2015 10:13
Show Gist options
  • Save LifeMoroz/b80be67da5c13a1b82d1 to your computer and use it in GitHub Desktop.
Save LifeMoroz/b80be67da5c13a1b82d1 to your computer and use it in GitHub Desktop.
class A(object): pass
a = A()
a.x = 1
setattr(a, 'y', 2)
a.__dict__ # {'y': 2, 'x': 1}
a.__setattr__('z', 3)
a.__dict__ # {'y': 2, 'x': 1, 'z': 3}
a.__setattr__ = lambda self: 42 # Заведомо неверно
a.__setattr__('z', 4) # TypeError
setattr(a, 'foo', 'bar') # ok
a.__dict__ #{'y': 2, 'x': 1, '__setattr__': <function <lambda> at 0x7fafa9b3a140>, 'z': 3, 'foo': 'bar'}
# А вот если так:
A.__setattr__ = lambda self: 42
# то
setattr(a, 'baz', 'quux') # Тут уже ошибка Type error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment