On Thu, Aug 12, 2021 at 3:01 AM Larry Hastings <la...@hastings.org> wrote: > > On 8/11/21 5:15 AM, Chris Angelico wrote: > > On Wed, Aug 11, 2021 at 10:03 PM Larry Hastings <la...@hastings.org> wrote: > > This approach shouldn't break reasonable existing code. That said, this > change would be observable from Python, and pathological code could notice > and break. For example: > > def ensure_Foo_is_a_class(o): > assert isinstance(Foo, type) > return o > > class Foo: > ... > > @ensure_Foo_is_a_class > def Foo(): > ... > > This terrible code currently would not raise an assertion. But if we made > the proposed change to the implementation of decorators, it would. I doubt > anybody does this sort of nonsense, I just wanted to fully flesh out the > topic. > > You would be here declaring that a @monkeypatch decorator is terrible > code. I'm not sure whether you're right or wrong. You may very well be > right. > > def monkeypatch(cls): > basis = globals()[cls.__name__] > for attr in dir(cls): setattr(basis, attr, getattr(cls, attr)) > return basis > > @monkeypatch > class SomeClass: > def new_method(self): ... > > Currently this works, since SomeClass doesn't get assigned yet. This > could be made to work across versions by writing it as > @monkeypatch(SomeClass) instead (and then the actual class name would > become immaterial). > > > Golly! I've never seen that. Is that a common technique? > > If we need to preserve that behavior, then this idea is probably a > non-starter. >
This specific thing? No, it's not common. But it's a natural consequence of the current behaviour, so if it does change, there'll be a variety of things that will break. It's hard to know how much of that is good code and how much is bad code. I have frequently made decorators that do unusual things (like returning non-functions) and I'm sure that some of them would be considered abuse of functionality :) ChrisA _______________________________________________ Python-Dev mailing list -- python-dev@python.org To unsubscribe send an email to python-dev-le...@python.org https://mail.python.org/mailman3/lists/python-dev.python.org/ Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/AJZEUIQTBOFS74CYAZ2R5WXBWRLDPF4X/ Code of Conduct: http://python.org/psf/codeofconduct/