
On Wed, Sep 14, 2016 at 2:16 AM, Pim Schellart p.schellart@princeton.edu wrote:
Semantics
The following two snippets are semantically identical::
continue class A: x = 5 def foo(self): pass def bar(self): pass
def foo(self): pass def bar(self): pass A.x = 5 A.foo = foo A.bar = bar del foo del bar
Did you know that you can actually abuse decorators to do this with existing syntax? Check out this collection of evil uses of decorators:
https://github.com/Rosuav/Decorators/blob/master/evil.py
I call them "evil" because they're potentially VERY confusing, but they're not necessarily bad. The monkeypatch decorator does basically what you're doing here, but with this syntax:
@monkeypatch class A: x = 5 def foo(self): pass def bar(self): pass
It's a little bit magical, in that it looks up the original class using globals(); this is partly deliberate, as it means you can't accidentally monkey-patch something from the built-ins, which will either fail, or (far worse) succeed and confuse everyone.
ChrisA