How to replace the metaclasses of all the classes imported
Jeff Epler
jepler at unpythonic.net
Tue Jun 3 10:45:18 EDT 2003
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
More information about the Python-list
mailing list