On 7/19/05, Kent Johnson <[EMAIL PROTECTED]> 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>',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.


_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to