How do I import variable packages in Python like using variable variables ($$) in PHP? -
How do I import variable packages in Python like using variable variables ($$) in PHP? -
i want import bundle depending on value user chooses.
the default file1.py
:
from files import file1
if user chooses file2
, should :
from files import file2
in php, can using variable variables:
class="lang-php prettyprint-override">$file_name = 'file1'; include($$file_name);
class="lang-php prettyprint-override">$file_name = 'file2'; include($$file_name);
how can in python?
python doesn't have feature that's straight equivalent php's "variable variables". "variable variable"'s value (or value of other expression) can utilize eval
function.
foo = "hello world" print eval("foo")
however, can't used in import
statement.
it possible utilize __import__
function import using variable.
package = "os" name = "path" imported = getattr(__import__(package, fromlist=[name]), name)
is equivalent to
from os import path imported
python variables variable-variables
Comments
Post a Comment