mercredi 23 septembre 2015

Python2 reflection: Strange behavior of __import__()

I need to have a generic script which takes some_module.function as argument and executes it. I wrote a solution for this (has to be Python-2.4 compatible...):

def get_function(f_name):
    """Return function from library..."""
    lib, f = f_name.rsplit('.', 1)
    module = getattr(__import__(lib), lib.rsplit('.', 1)[-1])
    return getattr(module, f)

f = get_function('my_libs.testlib.test_function')
# finally, this executes the function
f()

My question is:

Why do I have to do the getattr() after __import__()?

Turns out module = __import__('lib') will have the namespace above the one of lib.

So when I wanted to call a function from lib, say lib.f_x, I would have to do it like:

module = __import__('lib')
module.lib.f_x()

instead of what I would expect:

module = __import__('lib')
module.f_x()

Or use the contruct with getattr() as above. Why is that?





Aucun commentaire:

Enregistrer un commentaire