Chris Angelico added the comment:
The remaining difference that's actually of use, perhaps. But the decoration
itself happens before the name is bound. It's impossible to describe in Python
code; but it can be probed - you can monkeypatch a class using a decorator:
def monkeypatch(cls):
orig = globals()[cls.__name__] # Undocumented magic
print("Monkeypatch",id(cls),"into",id(orig))
for attr in dir(cls):
if not attr.startswith("_"):
setattr(orig,attr,getattr(cls,attr))
return orig
class Foo:
def method1(self):
print("I am method 1")
print("Foo is currently",id(Foo))
some_object = Foo()
@monkeypatch
class Foo:
def method2(self):
print("I am method 2")
print("Foo is now",id(Foo))
some_object.method1()
some_object.method2()
Is this undocumented behaviour? Should it be supported? It works on every
Python I've tried it on (CPython 2.7 and 3.6, PyPy2 and PyPy3, Jython, and
MicroPython), but it's not something I'd depend on in production code unless
it's documented.
----------
_______________________________________
Python tracker <[email protected]>
<http://bugs.python.org/issue26576>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com