a simple def how-to

V

vsoler

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=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?
 
J

John Posner

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=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?


var = [] # create empty list
for i in ranges:
var.append(readFromExcelRange(i))

-or-

var = [ readFromExcelRange(i) for i in ranges ]

-or-

var = map(readFromExcelRange, ranges)

-John
 
A

Andreas Waldenburger

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=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?


One additional line, and it works (all the following code is untested,
I might have goofed it up somewhere, but you get the idea):

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

Or, more succinctly:

var = dict((i, readFromExcelRange(i)) for i in ranges)

although that looks a bit crowded. Perhaps

rd = readFromExcelRange
var = dict((i, rd(i)) for i in ranges)

looks better, but not by much, IMO.

In Python 3 you can also just say

var = {i:readFromExcelRange(i) for i in ranges}

(I think. I don't have Python 3.) This looks comparatively neat,
because there are no nesting parens.


And just in case you think it's a good idea to meddle with globals and
create actual "variables": it's not. You absolutely want dictionaries
here. It's basically a bad idea to create names *implicitly* that
you're going to use *explicitly*. (That is, it is in Python anyway,
because it does not provide a clean and clear way of doing this. Other
languages might provide that sort of thing, and it might be awesome,
but in Python, no sir.)

/W
 
V

vsoler

My script starts like this:

But I would like to have something equivalent, like...
ranges=['book','house','table','read']
for i in ranges:
    var=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?

One additional line, and it works (all the following code is untested,
I might have goofed it up somewhere, but you get the idea):

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

Or, more succinctly:

var = dict((i, readFromExcelRange(i)) for i in ranges)

although that looks a bit crowded. Perhaps

rd = readFromExcelRange
var = dict((i, rd(i)) for i in ranges)

looks better, but not by much, IMO.

In Python 3 you can also just say

var = {i:readFromExcelRange(i) for i in ranges}

(I think. I don't have Python 3.) This looks comparatively neat,
because there are no nesting parens.

And just in case you think it's a good idea to meddle with globals and
create actual "variables": it's not. You absolutely want dictionaries
here. It's basically a bad idea to create names *implicitly* that
you're going to use *explicitly*. (That is, it is in Python anyway,
because it does not provide a clean and clear way of doing this. Other
languages might provide that sort of thing, and it might be awesome,
but in Python, no sir.)

/W


Thank you Andreas.

Your comprehensive answer makes a lot of sense to me.
 
V

vsoler

My script starts like this:

But I would like to have something equivalent, like...
ranges=['book','house','table','read']
for i in ranges:
     var=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?

var = []    # create empty list
for i in ranges:
     var.append(readFromExcelRange(i))

  -or-

var = [ readFromExcelRange(i) for i in ranges ]

  -or-

var = map(readFromExcelRange, ranges)

-John


John,

Thank you for your help. Perhaps the solution you are suggesting is
not exactly what I was looking for, but helped anyway.
 
J

John Posner

Thank you for your help. Perhaps the solution you are suggesting is
not exactly what I was looking for, but helped anyway.

Oops, I was thinking list, not dict. Too fast, and not enough coffee!

-John
 
S

Steven D'Aprano

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=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])
 
S

Stefan Behnel

vsoler, 07.03.2010 16:05:
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=readFromExcelRange(i)


Note that the name "i" is rather badly chosen as it generally implies a
totally different thing (integer) than what you use it for (names of ranges).

"ranges" seems to fall into the same bucket, but I guess that's just
because I can't extract the meaning from your code snippet (which is not a
good sign).

Try to use expressive names in your code, so that people who look at it for
the first time get an idea about what it does with what kind of data.

Stefan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,949
Members
47,500
Latest member
ArianneJsb

Latest Threads

Top