[issue47136] The variable __module__ in the class body getting an undesirable value from __prepare__ of the metaclass
Steven D'Aprano
report at bugs.python.org
Thu Apr 7 07:50:41 EDT 2022
Steven D'Aprano <steve at 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 <report at bugs.python.org>
<https://bugs.python.org/issue47136>
_______________________________________
More information about the Python-bugs-list
mailing list