Variable Substitution in commands or funtion names?

Chris Barker chrishbarker at home.net
Thu Jul 19 14:43:21 EDT 2001


fleet at teachout.org wrote:

> I think I may have gotten myself into a box.
> 
> I have a module "cust.py" that contains several dictionaries - each
> references a different customer with address info, etc. such as:
> 
> smith={"busname":"Smitties", "street":"123 West Ave"}

I think this is your problem right her. Having the customer name as a
variable name is problematic, it means the customer's names are
hard-coded into your module. You can do it, and some others have
suggested how(you might also want to look at eval and exec),but why not
use a nested dictionary?

Customers = {smith: {"busname":"Smitties", "street":"123 West Ave"},
             jones: {"busname":"Joneses", "street":"123 East Ave"},
              ....}

> from cust import CurCust
> 
> print cust.CurCust["street"]

now you do:

from cust import Customers
print Customers[CurCust]["street"]

or 
import cust
CurCust = cust.Customers[CurCustName]

print CurCust["street"]

> Spent the evening trying to solve this (Learning Python, and Sam's Teach
> Yourself Python) then tried the archives.  The little I found on "variable
> substitution" almost seems to say "no way!" ???

Python really doesn't do "variable substition the way TCL or *nix shells
do.

-Chris

-- 
Christopher Barker,
Ph.D.                                                           
ChrisHBarker at home.net                 ---           ---           ---
http://members.home.net/barkerlohmann ---@@       -----@@       -----@@
                                   ------@@@     ------@@@     ------@@@
Oil Spill Modeling                ------   @    ------   @   ------   @
Water Resources Engineering       -------      ---------     --------    
Coastal and Fluvial Hydrodynamics --------------------------------------
------------------------------------------------------------------------



More information about the Python-list mailing list