[Python-ideas] PEP 562

Nathaniel Smith njs at pobox.com
Tue Sep 12 15:32:26 EDT 2017


On Sep 12, 2017 7:08 AM, "Ionel Cristian Mărieș via Python-ideas" <
python-ideas at python.org> wrote:

Wouldn't a better approach be a way to customize the type of the module?
That would allow people to define behavior for almost anything (__call__,
__getattr__, __setattr__, __dir__, various operators etc). This question
shouldn't exist "why can't I customize behavior X in a module when I can do
it for a class". Why go half-way.


If you're ok with replacing the object in sys.modules then the ability to
totally customize your module's type has existed since the dawn era. And if
you're not ok with that, then it's still existed since 3.5 via the
mechanism of assigning to __class__ to change the type in-place. So this
discussion isn't about adding new functionality per se, but about trying to
find some way to provide a little bit of sugar that provides most of the
value in a less obscure way.

(And unfortunately there's a chicken and egg problem for using custom
module types *without* the __class__ assignment hack, because you can't
load any code from a package until after you've created the top level
module object. So we've kind of taken custom module types as far as they
can go already.)

-n
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20170912/5727647e/attachment.html>


More information about the Python-ideas mailing list