Another stab at a "switch/case" construct (for Python 3000):
Ken Peek
Ken.Peek at Engineer.com
Sat Mar 30 12:58:37 EST 2002
"Bjorn Pettersen" <BPettersen at NAREX.com> wrote in message news:<mailman.1017454367.25440.python-list at python.org>...
> > From: Steven Majewski [mailto:sdm7g at Virginia.EDU]
> >
> > On Fri, 29 Mar 2002, Bjorn Pettersen wrote:
> >
> > > > From: Steve Lamb [mailto:grey at despair.dmiyu.org]
> > > >
> > > > its called a directory pointing to functions = {'case':closed}
> > > >
> > > > I do not believe that any language which can have a hash
> > > > of function pointers has any business with a case statement.
> > >
> > > But that only works when all the functions in all the branches take
> the
> > > same number and types of arguments -- hardly the general case.
> > >
> >
> > table = {}
> >
> > table[key] = func, (args,...)
> > ...
> >
> > def doit( k ):
> > return table[k][0]( *table[k][1] )
>
> Point taken, but is this an argument for or against the match/switch
> statement <wink>?
>
> Personally, I don't think it's worth adding a match statement unless we
> can have ML style pattern matching...
>
> Not-sure-what-a-good-Pythonic-spelling-would-be-though'ly y'rs
> -- bjorn
Since we are no longer forcing only literals to be used (which would
allow for compile time optimizations), I don't think there would be
anything to stop you from allowing function calls (or other
expressions) in the parameter list of a clause, which would allow you
to do some pretty intricate calculations. This WOULD however redefine
the way lists are parsed, and _I_ don't like that:
branchon MyObject:
[123, 456, "hello", [], (), xyz-abc, 42*etc, MyFunc(MyObject)]:
# do some stuff
# do some more stuff
["I want a branchon construct", "(and more whining)"]:
# do some stuff
# do some more stuff
fallthrough # USEFUL a LOT!
[MyOtherObject, (1,2,3), [4,5,6]]:
# do some stuff
# do some more stuff
else:
# no matching clauses, so:
# do some stuff
# do some more stuff
def MyFunc(x):
# test object "x" for all kinds of things,
# and return "x" if you want a match,
# else return something guaranteed to
# fail, like (x,none)
if x_is_a_match:
return x
else
return (x,none)
=====================
This would be clear and readable. Use your imagination-- FEEL THE
POWER of this construct (to make more readable code)...
I-am-not-trying-to-impress-you-with-my-python-knowledge'ly yours,
--Ken Peek
=====================
More information about the Python-list
mailing list