Best way to enumerate something in python

Roy Smith roy at panix.com
Thu May 27 13:31:04 EDT 2004


In article <mailman.360.1085665812.6949.python-list at python.org>,
 "David Stockwell" <winexpert at hotmail.com> wrote:

> FWIW this is what I'm going to do for enumerated types in python.  Its not 
> perfect but it will make it fairly easy to get at column names so I can 
> build SQL statements on the fly and still have fairly easy to maintain code
> 
> #setup stuff
> NAME_COL,     ADDRESS_COL,    CITY_COL,  ZIPCODE_COL, \
> STATE_COL,    COUNTRY_COL,    SS_COL,    CAT_COL, \
> DATE_COL,     SALARY_COL                              = range(10)
> 
> mycol = {
>       NAME_COL: " NAME ",   ADDRESS_COL: " ADDRESS ",
>       CITY_COL: " CITY ",   ZIPCODE_COL: " ZIPCODE ",
>       STATE_COL:" STATE ", COUNTRY_COL:  " COUNTRY ",
>       SS_COL:   " SS ",       CAT_COL:   " CAT ",
>       DATE_COL: " DATE ",   SALARY_COL:  " SALARY " }
> # Use these for indexing by column name

I'm not sure what problem you're trying to solve here, but I suspect the 
cure is worse than the disease.  This seems like a lot of error-prone 
typing, and I'm not sure what the benefit is.

What happens when your SQL schema changes?  You need to make multiple 
changes to the above code to handle adding or deleting a column.

> print mycol[CITY_COL]

How is that any better than:

print "CITY"



More information about the Python-list mailing list