Steven D'Aprano <st...@pearwood.info> added the comment:
It would be nice if the class creation process was documented a bit better. Here is my experiment to see what is going on: ``` # the global `__name__` is normally the module's name. __name__ = "something" class Meta_default_prepare(type): def __new__(meta, name, bases, ns): print("ns for", name, "\n ", ns) return super().__new__(meta, name, bases, ns) class Meta_custom_prepare(Meta_default_prepare): def __prepare__(meta, *args): return {'__name__': 'another_name'} class Spam(metaclass=Meta_default_prepare): pass class Eggs(metaclass=Meta_custom_prepare): pass print("Spam module and name:", Spam.__module__, Spam.__name__) print("Eggs module and name:", Eggs.__module__, Eggs.__name__) ``` And the output in Python 3.10 is: ``` ns for Spam {'__module__': 'something', '__qualname__': 'Spam'} ns for Eggs {'__name__': 'another_name', '__module__': 'another_name', '__qualname__': 'Eggs'} Spam module and name: something Spam Eggs module and name: another_name Eggs ``` My take on this is that if the key __name__ is not present, the value of the class __module__ is taken from the global variable. So far so good. But if '__name__' is a key in the mapping returned by __prepare__, it gets left in the class dict, and gets used to set the class __module__ as well. But in neither case does it get used to set the class __name__. I cannot decide whether or not this makes sense to me. ---------- _______________________________________ Python tracker <rep...@bugs.python.org> <https://bugs.python.org/issue47136> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com