a simple def how-to

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sun Mar 7 17:42:59 CET 2010


On Sun, 07 Mar 2010 07:05:26 -0800, vsoler wrote:

> Hello,
> 
> My script starts like this:
> 
> book=readFromExcelRange('book')
> house=readFromExcelRange('house')
> table=readFromExcelRange('table')
> read=readFromExcelRange('read')
> ...
> 
> But I would like to have something equivalent, like...
> 
> ranges=['book','house','table','read'] 
> for i in ranges:
>     var[i]=readFromExcelRange(i)
> 
> which does not work. I assume I should be using globals() instead of
> var, but I do not know how to write my script.
> 
> Can anybody help?

Yes. Use a dict instead.

ranges=['book','house','table','read']
data = {}
for name in ranges:
    data[name] = readFromExcelRange(name)

# and later...

item = 'table'
print "The range '%s' is %s" % (item, data[item])



-- 
Steven



More information about the Python-list mailing list