[Tutor] globals and map()

Luis N tegmine at gmail.com
Tue Jul 19 13:59:36 CEST 2005


On 7/19/05, Kent Johnson <kent37 at tds.net> wrote:
> 
> Luis N wrote:
> > I'd appreciate some comments on my use of globals and the map function.
> >
> > import metakit
> > import marshal
> >
> > db = metakit.storage('addy.mk <http://addy.mk> <http://addy.mk>',1)
> > dbs = db.getas('dbs[db:S,marshal:B]')
> >
> > def li(i):
> > i = i[:-2]
> > return i
> >
> > def selectDB(f):
> > if f.has_key('db'):
> > d = []
> > d['db'] = f.get('db').value
> 
> The above line is not valid Python; list indices must be integers


> id = dbs.find(d)

> > if id != -1:
> > global desc, vw, primaryKey
> > desc = marshal.loads(dbs[id].marshal)
> > vw = db.getas('%s%s' % (dbs[id].db,desc))
> > desc = map(li,desc)
> 
> This use of map seems fine to me. You could also write it as a list 
> comprehension which is more mainstream Python idiom and maybe more readable 
> in this case as it is self-contained:
> desc = [ i[:-2] for i in desc ]
> 
> > primaryKey = desc[0]
> > return desc, vw, primaryKey
> 
> I don't see the need for the global declaration; desc, vw, primaryKey are 
> all assigned within this function and returned to the caller.


Kent



My blooper, should be d={} 

In this case consider desc, vw, and primaryKey to be of the same importance 
as dbs, other functions must access them in a way that renders their being 
local impossible, unless the logic where to be encapsulated in a class, 
which didn't seem necessary.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20050719/12e22e5b/attachment.htm


More information about the Tutor mailing list