How to replace the metaclasses of all the classes imported

Jane Austine janeaustine50 at hotmail.com
Tue Jun 3 14:34:53 EDT 2003


Jeff Epler <jepler at unpythonic.net> wrote in message news:<mailman.1054651567.32409.python-list at python.org>...
> I'll assume there's a function called 'set_metaclass(klass, new_meta)'
> that does what you want and returns the new class.  Also, you must pass
> in the right globals() or add the machinery to find the caller's global
> namespace. 
> 
> def import_with_metaclass_nonnested(module, names, new_meta, globals):
>     module_name = module.__name__
>     for item in names:
>         klass = getattr(module, item)
>         if not hasattr(klass, "__bases__"): continue
>         if klass.__module__ != module_name: continue
>         newklass = set_metaclass(klass, new_meta)
>         setattr(globals, item, newklass)
> 
> Of course, I would consider it an implementation detail whether a class
> documented as being from foomodule actually has __module__="foomodule".
> For instance, it might come from foomodule.internal_submodule, or from
> _foomodule, or from thirdparty_speedup_package.
> 
> I'm also not sure if the '__bases__' test for being a class is the right
> test.  The gnosis tools might have a better test that you could use
> instead.
> 
> Jeff

Let me explain more about what I want:

Let's assume there are a dozen of modules in my program.
##############################
#main.py

import alpha

class A:
    ....

class B:
    ....

def main(...):
    ....
    temp=alpha.C()
    ....

if __name__=='__main__':
    main(...)

##############################
#alpha.py

import beta

class C:
    ....

def ...

##############################
#beta.py

class D:
    ....

def ...

----------------------------------
What I'd like to do with metaclasses is, for example, log all
method calls with minimum code change.




More information about the Python-list mailing list