[Spambayes] A few code questions (Outlook oriented)

David LeBlanc whisper at oz.net
Wed Feb 19 02:52:53 EST 2003


> -----Original Message-----
> From: Moore, Paul [mailto:Paul.Moore at atosorigin.com]
> Sent: Wednesday, February 19, 2003 2:26
> To: David LeBlanc; Spambayes at Python. Org
> Subject: RE: [Spambayes] A few code questions (Outlook oriented)
>
>
> From: David LeBlanc [mailto:whisper at oz.net]
> > Now for the (probably) dumb question. I don't see how this works:
> >
> > #manager.py, line 159
> >         # determine which db manager to use, and create it.
> >         ManagerClass = [PickleStorageManager, DBStorageManager][use_db]
> >         self.db_manager = ManagerClass(bayes_base, mdb_base)
> >
> > ManagerClass is two lists, one has pointers to the two classes
> and the other
> > is the flag - and then it's called!?! eh? (I have no immediate plans to
> > change this (if I ever do), but I would like to understand
> what's going on.)
>
> [PickleStorageManager, DBStorageManager] is a list. You then *index* that
> list via [use_db] (it's the multiple meanings of [...] that are
> confusing).

Oh - duh. Never crossed my mind: I saw two lists.

> use_db is a boolean, taking values 0 or 1. So, the code is equivalent to
>
>     if use_db:
>         ManagerClass = DBStorageManager
>     else:
>         ManagerClass = PickleStorageManager
>
> You then call ManagerClass (which is one of the two relevant classes) to
> construct the manager object.

Yup yup.

> If you enjoyed this interlude, go and watch comp.lang.python,
> where there are
> currently over 1000 messages on various proposals for how to write C's
> conditional expression ( a ? b : c ) in Python. I'm sure this
> counts as some
> evidence in that debate, but it needs more evidence like a forest
> fire needs
> petrol...

It's getting my -1 unless it's "a ? b : c" and since Guido hates "?"... ;)

> (PS Mark, maybe you could rewrite the statement as a 4-line if, like I did
> above, just for clarity?)

What Mark said in his reply: dunt dumb down the code - comment it!

> > BTW, how does it get to reference a global (use_db) without a "global"
> > statement in the __init__ scope? I can't see where use_db gets
> used either?
>
> The "global" statement is only needed if you want to *update* the
> global. Read
> access to globals (when not shadowed by locals) is transparent.

Argh! really guys, I HAVE been programming Python for more than 2 weeks!
<blush> I thought if it went one way, it went the other and I've always been
after a global to update it, so that's why I never knew this. (Don't start
with the global updating - it's generally an init thing only!)

> Paul.

Thanks for the help gents. Mark, I'll be back to you when I get further into
the read status mod.

Regards,

Dave LeBlanc
Seattle, WA USA




More information about the Spambayes mailing list