Question: Dynamic code import

Lucio Torre lucio at movilogic.com
Thu Oct 25 16:59:31 EDT 2001


Károly Ladvánszky wrote:

>Hi Bjorn,
>
>Thanks for your quick answer. Yes, I was mistaken with the global directive.
>It's clear now.
>Regarding the first question, it works fine with the interpreter but I would
>like to read
>f11(a) from somewhere else, most likely from a file.
>
>Cheers,
>
>Károly
>
>

you can do this:

import f1
import f2

f =f1. f
f()
f=f2.f
f()

or mess with the imp module.
or, do the tricky thing:

---------------- f2.py
def f():
    print "f2"
---------------- f2.py

---------------- f.py
def f():
    print "f1"

fun = f
fun()

file = open("f2.py")
text = file.read()
exec(text)

fun = f2
fun()

---------------- f.py

but i think you sohuld stick with import.





More information about the Python-list mailing list