Metaclass question

Jp Calderone exarkun at
Wed Mar 12 20:10:29 CET 2003

On Wed, Mar 12, 2003 at 06:43:23PM +0000, Bojiro Kafir Tsava wrote:
> I need to achieve transparent import/instantiation of classes and modules.
> Here's a summary
> When I do:
> import
> should result in creation of a new python class "" which
> has a constructor:
> class MyClass:
>     def __init__(self, *args):
>         self.extObj =, "MyClass")

  This is something you want to use an import hook for, not a metaclass.

  Here is a very poor example:

    old_import = __import__
    def __import__(name, b, c, d):
        if name.startswith(''):
            return 'LA LA LA!'
        return old_import(name, b, c, d)
    __builtins__.__import__ = __import__

    import # Local name "foo" is now bound to "LA LA LA!"

  There is a more powerful and flexible mechanism in 2.3, but replacing
__import__ is probably all you need.

  Hope this helps,


 up 9 days, 11:59, 6 users, load average: 0.23, 0.25, 0.20

More information about the Python-list mailing list