[Tutor] globals and map()
Kent Johnson
kent37 at tds.net
Tue Jul 19 12:56:11 CEST 2005
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>',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
> else:
> error(-1)
> else:
> error(select=None)
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list