Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

python metaclasses: editing the namespace after `__set_name__` methods have been called?

Suppose we are defining a class with a metaclass.
In the class body, objects are assigned that implement __set_name__ to register themselves in a data structure of the class.

Is it possible to edit the namespace after __set_name__ methods have been run? Like, detach the filled data structure, split it in two, and add the parts under new attributes?

The trouble is that, before calling super().__new__(...) in the metaclass, the data structure is still empty. Afterwards it is filled, but then the class is already created, and its namespace is now a read-only copy of the namespace built up in the metaclass.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Is there any chance of editing the namespace, with __set_name__ methods applied, like via a hook just before it is frozen into the new class?

>Solution :

No. __set_name__ fundamentally cannot run before the class is created, because it takes the class as an argument.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading