@@ -1695,51 +1695,67 @@ Customizing module attribute access
16951695
16961696.. index ::
16971697 single: __getattr__ (module attribute)
1698+ single: __setattr__ (module attribute)
1699+ single: __delattr__ (module attribute)
16981700 single: __dir__ (module attribute)
16991701 single: __class__ (module attribute)
17001702
1701- Special names ``__getattr__ `` and ``__dir__ `` can be also used to customize
1702- access to module attributes. The ``__getattr__ `` function at the module level
1703+ Special names ``__getattr__ ``, ``__setattr__ ``, ``__delattr__ `` and ``__dir__ ``
1704+ can be also used to customize access to module attributes.
1705+
1706+ The ``__getattr__ `` function at the module level
17031707should accept one argument which is the name of an attribute and return the
17041708computed value or raise an :exc: `AttributeError `. If an attribute is
17051709not found on a module object through the normal lookup, i.e.
17061710:meth: `object.__getattribute__ `, then ``__getattr__ `` is searched in
17071711the module ``__dict__ `` before raising an :exc: `AttributeError `. If found,
17081712it is called with the attribute name and the result is returned.
17091713
1714+ The ``__setattr__ `` function should accept two arguments, respectively, the
1715+ name of an attribute and the value to be assigned and return :const: `None ` or
1716+ raise an :exc: `AttributeError `. If present, this function overrides the
1717+ standard :func: `setattr ` behaviour on a module. For example::
1718+
1719+ def __setattr__(name, value):
1720+ print(f'Setting {name} to {value}...')
1721+ globals()[name] = value
1722+
1723+ The ``__delattr__ `` function should accept one argument which is the name of an
1724+ attribute and return :const: `None ` or raise an :exc: `AttributeError `. If present,
1725+ this function overrides the standard :func: `delattr ` behaviour on a module.
1726+
17101727The ``__dir__ `` function should accept no arguments, and return a sequence of
17111728strings that represents the names accessible on module. If present, this
17121729function overrides the standard :func: `dir ` search on a module.
17131730
1714- For a more fine grained customization of the module behavior (setting
1715- attributes, properties, etc.), one can set the `` __class__ `` attribute of
1716- a module object to a subclass of :class: `types.ModuleType `. For example::
1731+ For a more fine grained customization of the module behavior, one can set the
1732+ `` __class__ `` attribute of a module object to a subclass of
1733+ :class: `types.ModuleType `. For example::
17171734
17181735 import sys
17191736 from types import ModuleType
17201737
1721- class VerboseModule(ModuleType):
1722- def __repr__(self):
1723- return f'Verbose {self.__name__}'
1724-
1725- def __setattr__(self, attr, value):
1726- print(f'Setting {attr}...')
1727- super().__setattr__(attr, value)
1738+ class CallableModule(ModuleType):
1739+ def __call__(self, *args, **kwargs):
1740+ raise RuntimeError("NO-body expects the Spanish Inquisition!")
17281741
1729- sys.modules[__name__].__class__ = VerboseModule
1742+ sys.modules[__name__].__class__ = CallableModule
17301743
17311744.. note ::
1732- Defining module ``__getattr__ `` and setting module `` __class__ `` only
1733- affect lookups made using the attribute access syntax -- directly accessing
1734- the module globals (whether by code within the module, or via a reference
1735- to the module's globals dictionary) is unaffected.
1745+ Defining module ``__getattr__ ``/`` __setattr__ ``/`` __delattr__ `` and setting
1746+ module `` __class__ `` only affect lookups made using the attribute access
1747+ syntax -- directly accessing the module globals (whether by code within the
1748+ module, or via a reference to the module's globals dictionary) is unaffected.
17361749
17371750.. versionchanged :: 3.5
17381751 ``__class__ `` module attribute is now writable.
17391752
17401753.. versionadded :: 3.7
17411754 ``__getattr__ `` and ``__dir__ `` module attributes.
17421755
1756+ .. versionadded :: 3.13
1757+ ``__setattr__ `` and ``__delattr__ `` module attributes.
1758+
17431759.. seealso ::
17441760
17451761 :pep: `562 ` - Module __getattr__ and __dir__
0 commit comments