[Python-Dev] module customization (original) (raw)

Nathaniel Smith njs at pobox.com
Thu Nov 16 01:14:20 EST 2017


On Wed, Nov 15, 2017 at 4:27 PM, Ethan Furman <ethan at stoneleaf.us> wrote:

The second way is fairly similar, but instead of replacing the entire sys.modules entry, its class is updated to be the class just created -- something like sys.modules['mymod'].class = MyNewClass .

My request: Can someone write a better example of the second method? And include getattr ?

Here's a fairly straightforward example:

https://github.com/python-trio/trio/blob/master/trio/_deprecate.py#L114-L140

(Intentionally doesn't include dir because I didn't want deprecated attributes to show up in tab completion. For other use cases like lazy imports, you would implement dir too.)

Example usage:

https://github.com/python-trio/trio/blob/master/trio/init.py#L66-L98

-n

-- Nathaniel J. Smith -- https://vorpus.org



More information about the Python-Dev mailing list