A little disappointed so far

Aahz aahz at pythoncraft.com
Sun May 18 22:51:53 EDT 2003


In article <ubWxa.292$573.155 at news-binary.blueyonder.co.uk>,
Graham Nicholls  <graham at rockcons.co.uk> wrote:
>
>No case statement - or is it simply that my documentation is out of date? I
>_know_ I can do if elif else constructs, but in a language which prides
>itself in its readability, this is laughable.

The Pythonic approach is to use a dict to index into a set of functions:

def handle_dir(name):
    <something>

def handle_file():
    <something else>

selector = {
    'file': handle_file,
    'dir': handle_dir,
    }

name_type = get_name_type(name)
selector[name_type](name)

Note carefully the lack of parens in the selector setup and the later
parens as you *use* the dict.
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

"In many ways, it's a dull language, borrowing solid old concepts from
many other languages & styles:  boring syntax, unsurprising semantics,
few automatic coercions, etc etc.  But that's one of the things I like
about it."  --Tim Peters on Python, 16 Sep 93




More information about the Python-list mailing list