Python: Making Imports Callable

I wanted to do

import module

I thought this did not exist in Python but Chris Angelico, a Python core dev showed me it existed!

import sys, types
class Module(types.ModuleType):
    def __call__(self):
        print("You called me!")
sys.modules[__name__].__class__ = Module

import importme

Mind boggling!

Leave a Comment

Your email address will not be published. Required fields are marked *