On Tue, Oct 6, 2020 at 9:49 PM Random832 <random...@fastmail.com> wrote:
> On Tue, Oct 6, 2020, at 02:50, Alperen Keleş wrote: > > I think a class type such as "@functionclass" may be helpful for > > creating functions intended to keep a list of methods in a scope. > > > > At the moment, I achieved this via writing "@classmethod" to all my > > functions but I think such a decorator might help clarify intent for > > the reader and ease the programmers' job. > > I think new syntax would be better than a decorator (or a metaclass, which > for some reason never seems to get suggested for these things), I think maybe discussion of the general namespace idea might be a little off topic for this read (per Guido's comments). However the metaclass below-- or something like it-- could still help with the problem? It was actually my attempt at a general namespace object in the previous namespace thread. It seemed to work ok, and it should work in this use case: import types class namespace(type): def __new__(mcls, name, bases, dct): if bases: raise TypeError("this is a namespace, not a class.") mod = types.ModuleType(dct.pop("__qualname__"), dct.pop("__doc__",None)) mod.__dict__.update(dct) return mod class ns(metaclass=namespace): class Desc: def __get__(self, obj, cls): return None d = Desc() if __name__=="__main__": assert ns.d # success: descriptor protocol broken as expected But there are probably deficiencies I have not uncovered. --- Ricky. "I've never met a Kentucky man who wasn't either thinking about going home or actually going home." - Happy Chandler
_______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/4OVMEZM5DQCKJUPMNNJQ7RMNBLFAPSAP/ Code of Conduct: http://python.org/psf/codeofconduct/