Question: Dynamic code import

Károly Ladvánszky aa at bb.cc
Thu Oct 25 18:35:46 EDT 2001


Thanks indeed! 'exec' might be the answer to my question. 'import' is not ok
as the input
file may not exist at all when the script starts running.

Cheers

Károly

"Lucio Torre" <lucio at movilogic.com> az alábbiakat írta a következo
üzenetben: news:mailman.1004043866.29873.python-list at python.org...
> 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.
>
>


______________________________________________________________________________
Posted Via Binaries.net = SPEED+RETENTION+COMPLETION = http://www.binaries.net



More information about the Python-list mailing list