| From: | "Jay O'Connor" <joconnor(at)cybermesa(dot)com> | 
|---|---|
| To: | pgsql-general(at)postgresql(dot)org | 
| Subject: | Re: language war | 
| Date: | 2003-12-02 16:45:15 | 
| Message-ID: | 3FCCC19B.7080802@cybermesa.com | 
| Views: | Whole Thread | Raw Message | Download mbox | Resend email | 
| Thread: | |
| Lists: | pgsql-general | 
 >>My favorite feature is the fact that I can include
 > >arbitrary files, using a line of code:
 > >include "$next_file.php";
 >Dynamically importing modules based on variables like you show above is
 >certainly possible in Python.
Here's an elaborate example:  I have two modules (A.py and B.py) that 
both define the function test().  My main driver prompts the user for 
input and based on the input will import the module and call test() in 
the imported module, which will print either "module A" or "module B" 
depending on what was imported, or an error message if the input didn't 
matcha  module.  This illustrates a complete example, but you can 
simplify it really to "mymodule = __import__ (next_file)" where 
next_file is a variable name
====================
--A.py
def test ():
        print "module A"
--B.py
def test ():
        print "module B"
--dynamicimport.py
print "Test Dynamic Import"
request = raw_input ("enter 'a' or 'b'")
modName = request.upper()
mod = None
try:
    mod = __import__(modName)
except:
    print "unable to import %s" % modName
if mod:
    mod.test()
else:
    print "Sorry, no appropriate module"
====================
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Richard Huxton | 2003-12-02 17:12:51 | Re: how many quotes? | 
| Previous Message | Jay O'Connor | 2003-12-02 16:21:33 | Re: language war |