Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions Doc/reference/datamodel.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1500,6 +1500,39 @@ access (use of, assignment to, or deletion of ``x.name``) for class instances.
returned. :func:`dir` converts the returned sequence to a list and sorts it.


Customizing module attribute access
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. index::
single: __class__ (module attribute)

For a more fine grained customization of the module behavior (setting
attributes, properties, etc.), one can set the ``__class__`` attribute of
a module object to a subclass of :class:`types.ModuleType`. For example::

import sys
from types import ModuleType

class VerboseModule(ModuleType):
def __repr__(self):
return f'Verbose {self.__name__}'

def __setattr__(self, attr, value):
print(f'Setting {attr}...')
setattr(self, attr, value)

sys.modules[__name__].__class__ = VerboseModule

.. note::
Setting module ``__class__`` only affects lookups made using the attribute
access syntax -- directly accessing the module globals (whether by code
within the module, or via a reference to the module's globals dictionary)
is unaffected.

.. versionchanged:: 3.5
``__class__`` module attribute is now writable.


.. _descriptors:

Implementing Descriptors
Expand Down