New submission from Douglas Raillard <[email protected]>:
The cell object __classcell__ currently cannot be set by code invoked by
metaclass.__new__. Attempts to do so will be caught by builtin___build_class__
in bltimodule.c:
} else {
const char *msg =
"__class__ set to %.200R defining %.200R as %.200R";
PyErr_Format(PyExc_TypeError, msg, cell_cls, name, cls);
}
This is unfortunate as there is a use-case for such trickery: if the method of
a class A are only used to be grafted onto another class B (monkey patching),
A.__classcell__ should be set to B so that super() works as expected.
This can be useful in contexts where e.g. A.__new__ returns instances of B. B
is not necessarily something under direct user control (it can be dynamically
created by inheriting from a "template"), so A would be a better place for
users to define custom method.
----------
components: Interpreter Core
messages: 416172
nosy: douglas-raillard-arm
priority: normal
severity: normal
status: open
title: Allow setting __classcell__
versions: Python 3.11
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue47144>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com