A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://mail.python.org/pipermail/python-list/2010-March/720987.html below:

a simple def how-to

a simple def how-toSteven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sun Mar 7 11:42:59 EST 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

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4