[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

a simple def how-to

vsoler

3/7/2010 3:05:00 PM

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

John Posner

3/7/2010 3:24:00 PM

0

On 3/7/2010 10:05 AM, 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?

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

Andreas Waldenburger

3/7/2010 3:24:00 PM

0

On Sun, 7 Mar 2010 07:05:26 -0800 (PST) vsoler
<vicente.soler@gmail.com> 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?

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[i]=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

--
INVALID? DE!

vsoler

3/7/2010 3:58:00 PM

0

On 7 mar, 16:23, Andreas Waldenburger <use...@geekmail.INVALID> wrote:
> On Sun, 7 Mar 2010 07:05:26 -0800 (PST) vsoler
>
>
>
> <vicente.so...@gmail.com> 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?
>
> 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[i]=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
>
> --
> INVALID? DE!

Thank you Andreas.

Your comprehensive answer makes a lot of sense to me.

vsoler

3/7/2010 4:00:00 PM

0

On 7 mar, 16:23, John Posner <jjpos...@optimum.net> wrote:
> On 3/7/2010 10:05 AM, 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?
>
> 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.

John Posner

3/7/2010 4:17:00 PM

0

On 3/7/2010 10:59 AM, vsoler wrote:
>
> 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


Steven D'Aprano

3/7/2010 4:43:00 PM

0

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

Stefan Behnel

3/7/2010 5:17:00 PM

0

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[i]=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