Case Statements
Antoon Pardon
antoon.pardon at rece.vub.ac.be
Thu Mar 17 05:23:23 EDT 2016
Op 17-03-16 om 03:02 schreef Chris Angelico:
> On Thu, Mar 17, 2016 at 12:54 PM, Steven D'Aprano <steve at pearwood.info> wrote:
>
>> I wouldn't want to rely on it working with decorator syntax either. Even if
>> it does now, I'm not sure that's a language guarantee.
> That's the thing, though. It's not a guarantee, yet it does work in
> every Python interpreter that I tried it in.
That depends on what you mean by work. This failes:
def monkeypatch(cls):
orig = globals()[cls.__name__]
print("Monkeypatch",id(cls),"into",id(orig))
for attr in dir(cls):
if not attr.startswith("_"):
setattr(orig,attr,getattr(cls,attr))
return orig
def main():
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()
main()
More information about the Python-list
mailing list