OT: Re: Just took a look in the perl newsgroup....

Michael Chermside mcherm at mcherm.com
Wed May 28 15:21:55 EDT 2003


[Michael Chermside now realizes that indexing a dict of functions
  isn't a drop-in replacement for a "case" statement because it
  can't rebind variables local to the function containing the
  "case"]

Bengt Richter replies:
> But I just thought of an alternative
    [...]
>        try: raise `x`
>        except '1':
>            # case code in same scope as case
>            inner_var = '<<value bound in scope of "case" 1)>>'
>        except '2':
>            inner_var = '<<value bound in scope of "case" 2>>'
>        except:
>            inner_var = '<<value bound in scope of "case" default>>'

Yes, but I would NEVER use that. First of all, I'm looking for
something I can show to newbies. For myself, I either use
if-elif-else or I refactor to create a class and use method
dispatch. But I figured that in addition to if-elif-else, I'd
mention dicts of functions to newbies, because
  (1) It would keep them from griping that "if-elif-else is
      just a bunch of if statements, it's not a case statement!"
  (2) It might get them thinking about a more powerful
      technique for solving their problem.

But I'd certainly never propose the except trick. Although I
must admit it makes a nice evil hack. Particularly with the
use of backticks (thus hitting at least two oughta-be-deprecated
features at once!).

Evil and cool - not useful to me, but fun to see it work.

Now-waiting-for-case-implemented-as-a-metaclass-hack lly yours,

-- Michael Chermside






More information about the Python-list mailing list