Another stab at a "switch/case" construct (for Python 3000):

Bengt Richter bokr at oz.net
Thu Mar 28 16:39:10 EST 2002


On Thu, 28 Mar 2002 07:52:45 -0800, "Ken Peek" <Ken.Peek at SpiritSongDesigns.comNOSPAM> wrote:

>Well-- OK, my new ISP doesn't have a news server, but I found one
>that I could use to post this with.  If you reply to my email, do
>the 'nospam' thing first to my email address...
>
>I really am interested in what people think about this.  I have
>looked at all the other c.l.py posts on this construct, but I
>don't think anyone proposed this format.
>
>BTW-- the 'until' thing in the original letter was already posted,
>so please disregard that part of this post- (I left it in for
>accuracy.)
>
>  -----Original Message-----
>  From: Guido van Rossum
>  Sent: Thursday, March 28, 2002 04:45
>  To: Ken.Peek at SpiritSongDesigns.NOSPAM.com
>  Cc: Marc-Andre Lemburg
>  Subject: Re: PEP 0275
>
>  If you really want to discuss this, it's better to post to c.l.py.
>
>  --Guido van Rossum (home page: http://www.python.org/~guido/)
>
>  -----Original Message-----
>  From: Ken Peek [mailto:Ken.Peek at SpiritSongDesigns.NOSPAM.com]
>  Sent: Thursday, March 28, 2002 01:05
>  To: Marc-Andre Lemburg
>  Cc: Guido van Rossum
>  Subject: PEP 0275
>
>  How about:
>
>  match some_object:
>
>      ("I dream of Jeanie",):
>          print "with the light brown hair"
>
>      (1,3,5,7):
>
>          print "some odd numbers"
>
>          # 'break' leaves the entire match structure
>          if (time_to_leave): break
>
>          # 'continue' jumps to the first
>          # statement in the next clause
>          continue
>
>      (123,456,789):
>
>          print "some bigger numbers too"
>
>      (19,56,22):
>
>          print "some random numbers"
>
>      (1001,2002,3000):
>          # YES! It IS possible we want to do NOTHING!
>          pass
>
>      any:
>          print "we got here because there were no matches"
>
===========================================
[13:32] C:\pywk\misc>python case.py "I dream of Jeanie"
with the light brown hair

[13:36] C:\pywk\misc>python case.py 5
some odd numbers

[13:36] C:\pywk\misc>python case.py 456
some bigger numbers too

[13:36] C:\pywk\misc>python case.py 22
some random numbers

[13:36] C:\pywk\misc>python case.py 2002

[13:36] C:\pywk\misc>python case.py xxx
we got here because there were no matches

from following code:
===========================================
def main(some_object):
    class CkObjIn:
        """functor class: instance holds object for check if in passed sequence"""
        def __init__(self,obj):
            self.obj = obj
        def __call__(self, *seq):
            return self.obj in seq

    ## now use it
    ck = CkObjIn(some_object)    # instead of match line

    if ck("I dream of Jeanie",):
        print "with the light brown hair"

    elif ck(1,3,5,7):

        print "some odd numbers"

        ###### no: there should be now new meaning here ######
        # 'break' leaves the entire match structure
        # 'continue' jumps to the first
        # statement in the next clause
        ###### no: there should be now new meaning here ######

    elif ck(123,456,789):

        print "some bigger numbers too"

    elif ck(19,56,22):

        print "some random numbers"

    elif ck(1001,2002,3000):

        # YES! It IS possible we want to do NOTHING!
        pass

    else:
        print "we got here because there were no matches"

if __name__ == '__main__':
    import sys
    try:
        arg = int(sys.argv[1])
    except:
        arg = sys.argv[1]
    main(arg)
===========================================
[...]
>
>  Well, what do you think?
>
Kind of like it, but there would have to be some hot
performance advantage. IMO the aesthetics aren't that
much different from the above.

I definitiely wouldn't like break and continue to mean new things!

Regards,
Bengt Richter



More information about the Python-list mailing list