[Python-3000] Switch and static, redux

Jim Jewett jimjjewett at gmail.com
Wed Jul 12 21:32:36 CEST 2006


On 7/7/06, Guido van Rossum <guido at python.org> wrote:
> I don't like storing it as a global; and that's probably incorrect
> too, e.g. if multiple definitions of f are being made in a loop.

Should the case expressions be different for each loop?  If not, then
it doesn't matter, and a per-module storage is fine, and offers the
best optimization.  (It may not be the easiest initial implementation,
but that is no reason to rule it out forever.)

If the case expressions can differ on each iteration, then you're
supporting non-constant expressions.   How important is this use case?
 Important enough to slow down all nested switches and complicate the
(detailed) semantics?


> I guess the problem (with this continued argument) is that I'm
> constantly switching between two points of view, and need both to
> decide on an optimal solution. One POV is that of the casual user of
> the switch statement. They should stick to the "use only things that
> are (conceptually) constants".

Aside from AttributeError/NameError, this case doesn't care when the
freeze happens.

> The other POV is that of the expert
> user trying to understand (accidentally) obfuscated code. I still like
> there to be simple rules to rememver for the latter. The difference is
> that the rules don't have to be obvious unless you're somewhat
> familiar with the implementation (in this case, storing the dispatch
> table on the nearest function object).

If you're going to require familiarity with the implementation anyhow,
then you can use almost arbitrary rules.  Why not store the switch in
a module-level cellvar that gets set at most once?

-jJ


More information about the Python-3000 mailing list