Is there no switch function in Python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rudi Hansen

    #1

    Is there no switch function in Python

    I dont seem to be able to find the switch statement in Python.

    I would like to be able to do

    switch(var)
    case 1 :
    print "var = 1"
    case 2:
    print "var = 2"

    But it seems that i have to do.

    if(var=1)
    print "var =1"
    elseif(var=2)
    print "var=2"

    Is ther no easier way??


  • Marius Bernklev

    #2
    Re: Is there no switch function in Python

    "Rudi Hansen" <rsh_remove_thi s_@pobox.dk> writes:
    [color=blue]
    > I would like to be able to do
    >
    > switch(var)
    > case 1 :
    > print "var = 1"
    > case 2:
    > print "var = 2"
    >
    > But it seems that i have to do.
    >
    > if(var=1)
    > print "var =1"
    > elseif(var=2)
    > print "var=2"
    >
    > Is ther no easier way??[/color]

    one way is using dicts:

    swoosh = {1: "var = 1", 2: "var = 2"}
    print swoosh[var]


    --
    Marius Bernklev

    Comment

    • Alex Martelli

      #3
      Re: Is there no switch function in Python

      Rudi Hansen <rsh_remove_thi s_@pobox.dk> wrote:
      [color=blue]
      > I dont seem to be able to find the switch statement in Python.[/color]

      Right, there isn't one.
      [color=blue]
      >
      > I would like to be able to do
      >
      > switch(var)
      > case 1 :
      > print "var = 1"
      > case 2:
      > print "var = 2"
      >
      > But it seems that i have to do.
      >
      > if(var=1)
      > print "var =1"
      > elseif(var=2)
      > print "var=2"
      >
      > Is ther no easier way??[/color]

      several, starting with

      if var in (1,2): print 'var = %s' % var

      The most Pythonic idiom, when you have to do something substantial in
      each branch of the switch, is a dictionary of callables -- in this toy
      case it might be:

      switch = {1: lambda: sys.stdout.writ e('var=1\n'),
      2: lambda: sys.stdout.writ e('var=2\n'), }
      switch.get(var, lambda: '')()

      An if/elif tree is also fine, though the syntax is not as you think...:

      if var == 1:
      print 'var is one'
      elif var == 2:
      print 'var is two'



      Alex

      Comment

      • Sion Arrowsmith

        #4
        Re: Is there no switch function in Python

        In article <CR20d.46131$Vf .2380522@news00 0.worldonline.d k>,
        Rudi Hansen <rsh_remove_thi s_@pobox.dk> wrote:[color=blue]
        >I dont seem to be able to find the switch statement in Python.
        >
        >I would like to be able to do
        >
        >switch(var)
        > case 1 :
        > print "var = 1"
        > case 2:
        > print "var = 2"[/color]

        5 lines, 55 characters including the print statements.

        [color=blue]
        >But it seems that i have to do.
        >
        >if(var=1)
        > print "var =1"
        >elseif(var=2 )
        > print "var=2"[/color]

        4 lines, 52 characters (once corrected) including the print
        statements (as written in the first example).
        [color=blue]
        >Is ther no easier way??[/color]

        In what way do you want it to be "easier"?

        --
        \S -- siona@chiark.gr eenend.org.uk -- http://www.chaos.org.uk/~sion/
        ___ | "Frankly I have no feelings towards penguins one way or the other"
        \X/ | -- Arthur C. Clarke
        her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump

        Comment

        • Roy Smith

          #5
          Re: Is there no switch function in Python

          In article <1Zb*VOguq@news .chiark.greenen d.org.uk>,
          Sion Arrowsmith <siona@chiark.g reenend.org.uk> wrote:
          [color=blue]
          > In article <CR20d.46131$Vf .2380522@news00 0.worldonline.d k>,
          > Rudi Hansen <rsh_remove_thi s_@pobox.dk> wrote:[color=green]
          > >I dont seem to be able to find the switch statement in Python.
          > >
          > >I would like to be able to do
          > >
          > >switch(var)
          > > case 1 :
          > > print "var = 1"
          > > case 2:
          > > print "var = 2"[/color]
          >
          > 5 lines, 55 characters including the print statements.[/color]

          It's even longer if you include the required "break" statement at the
          end of case 1 (assuming you're talking about a C-style switch statement
          with fall-through cases).

          That being said, there are times when I miss switch. For some kinds of
          multi-branch logic, I think it expresses the meaning better than a
          string of if's. But it's hardly necessary.

          The real reason, IMHO, switch existed in C was because it would let the
          compiler build very efficient jump tables for switches with many cases.
          Imagine switching on the ascii value of a character and having a 128
          different cases. The compiler could build a 128 entry jump table and
          the switch statement would compile down to a single machine instruction.

          Comment

          • Isaac To

            #6
            Re: Is there no switch function in Python

            >>>>> "Roy" == Roy Smith <roy@panix.co m> writes:

            Roy> The real reason, IMHO, switch existed in C was because it
            Roy> would let the compiler build very efficient jump tables for
            Roy> switches with many cases. Imagine switching on the ascii
            Roy> value of a character and having a 128 different cases. The
            Roy> compiler could build a 128 entry jump table and the switch
            Roy> statement would compile down to a single machine instruction.

            C switch is much more flexible than one would expect. It is not just
            a cheap replacement of a sequence of if-then-else. E.g., in an
            exercise of "The C++ Programming Language" of Bjarne Stroustrup, you
            can see the following example code:

            void send(int *to, int *from, int count) {
            int n = (count + 7) / 8;
            switch (count % 8) {
            case 0: do { *to++ = *from++;
            case 7: *to++ = *from++;
            case 6: *to++ = *from++;
            case 5: *to++ = *from++;
            case 4: *to++ = *from++;
            case 3: *to++ = *from++;
            case 2: *to++ = *from++;
            case 1: *to++ = *from++;
            } while (--n > 0);
            }
            }

            Note that this reduces the number of branch statements to execute
            8-folds for any compiler but the smartest (as compared to the simplest
            code "do { *to++ = *from++ } while (--count > 0);").

            I'd instead guess that C has switch because at the times before C,
            people code in assembly, and those tricks are popular. The design of
            most programming language represents the norm of coding at that time.

            Regards,
            Isaac.

            Comment

            • Cameron Laird

              #7
              Re: Is there no switch function in Python

              In article <roy-03E598.09352710 092004@reader1. panix.com>,
              Roy Smith <roy@panix.co m> wrote:

              Comment

              • Roy Smith

                #8
                Re: Is there no switch function in Python

                Isaac To <iketo2@netscap e.net> wrote:[color=blue]
                > C switch is much more flexible than one would expect. It is not just
                > a cheap replacement of a sequence of if-then-else. E.g., in an
                > exercise of "The C++ Programming Language" of Bjarne Stroustrup, you
                > can see the following example code:
                >
                > void send(int *to, int *from, int count) {
                > int n = (count + 7) / 8;
                > switch (count % 8) {
                > case 0: do { *to++ = *from++;
                > case 7: *to++ = *from++;
                > case 6: *to++ = *from++;
                > case 5: *to++ = *from++;
                > case 4: *to++ = *from++;
                > case 3: *to++ = *from++;
                > case 2: *to++ = *from++;
                > case 1: *to++ = *from++;
                > } while (--n > 0);
                > }
                > }[/color]

                La-de-dah, Bjarne discovers loop unrolling. You don't need a switch
                statement to unroll loops.

                Hold on a second. Cases 1 through 7 jump into the middle of the do
                loop!? Pardon me while I barf. I didn't even know that was legal.
                That's the kind of code that gives C++ a bad name.

                Comment

                • Jaime Wyant

                  #9
                  Re: Is there no switch function in Python

                  Don't blame c++. This is a relic of 'C'.





                  On Fri, 10 Sep 2004 11:16:12 -0400, Roy Smith <roy@panix.co m> wrote:[color=blue]
                  > Isaac To <iketo2@netscap e.net> wrote:[color=green]
                  > > C switch is much more flexible than one would expect. It is not just
                  > > a cheap replacement of a sequence of if-then-else. E.g., in an
                  > > exercise of "The C++ Programming Language" of Bjarne Stroustrup, you
                  > > can see the following example code:
                  > >
                  > > void send(int *to, int *from, int count) {
                  > > int n = (count + 7) / 8;
                  > > switch (count % 8) {
                  > > case 0: do { *to++ = *from++;
                  > > case 7: *to++ = *from++;
                  > > case 6: *to++ = *from++;
                  > > case 5: *to++ = *from++;
                  > > case 4: *to++ = *from++;
                  > > case 3: *to++ = *from++;
                  > > case 2: *to++ = *from++;
                  > > case 1: *to++ = *from++;
                  > > } while (--n > 0);
                  > > }
                  > > }[/color]
                  >
                  > La-de-dah, Bjarne discovers loop unrolling. You don't need a switch
                  > statement to unroll loops.
                  >
                  > Hold on a second. Cases 1 through 7 jump into the middle of the do
                  > loop!? Pardon me while I barf. I didn't even know that was legal.
                  > That's the kind of code that gives C++ a bad name.
                  >
                  >
                  > --
                  > http://mail.python.org/mailman/listinfo/python-list
                  >[/color]

                  Comment

                  • Peter Otten

                    #10
                    Re: Is there no switch function in Python

                    Isaac To wrote:
                    [color=blue]
                    > a cheap replacement of a sequence of if-then-else. E.g., in an
                    > exercise of "The C++ Programming Language" of Bjarne Stroustrup, you
                    > can see the following example code:
                    >
                    > void send(int *to, int *from, int count) {
                    > int n = (count + 7) / 8;
                    > switch (count % 8) {
                    > case 0: do { *to++ = *from++;
                    > case 7: *to++ = *from++;
                    > case 6: *to++ = *from++;
                    > case 5: *to++ = *from++;
                    > case 4: *to++ = *from++;
                    > case 3: *to++ = *from++;
                    > case 2: *to++ = *from++;
                    > case 1: *to++ = *from++;
                    > } while (--n > 0);
                    > }
                    > }[/color]

                    This is otherwise known a Duff's device:



                    Peter

                    Comment

                    • Bengt Richter

                      #11
                      Re: Is there no switch function in Python

                      On Thu, 9 Sep 2004 22:27:02 +0200, aleaxit@yahoo.c om (Alex Martelli) wrote:
                      [color=blue]
                      >Rudi Hansen <rsh_remove_thi s_@pobox.dk> wrote:
                      >[color=green]
                      >> I dont seem to be able to find the switch statement in Python.[/color]
                      >
                      >Right, there isn't one.
                      >[color=green]
                      >>
                      >> I would like to be able to do
                      >>
                      >> switch(var)
                      >> case 1 :
                      >> print "var = 1"
                      >> case 2:
                      >> print "var = 2"
                      >>
                      >> But it seems that i have to do.
                      >>
                      >> if(var=1)
                      >> print "var =1"
                      >> elseif(var=2)
                      >> print "var=2"
                      >>
                      >> Is ther no easier way??[/color]
                      >
                      >several, starting with
                      >
                      >if var in (1,2): print 'var = %s' % var
                      >
                      >The most Pythonic idiom, when you have to do something substantial in
                      >each branch of the switch, is a dictionary of callables -- in this toy
                      >case it might be:
                      >
                      >switch = {1: lambda: sys.stdout.writ e('var=1\n'),
                      > 2: lambda: sys.stdout.writ e('var=2\n'), }
                      >switch.get(var , lambda: '')()
                      >
                      >An if/elif tree is also fine, though the syntax is not as you think...:
                      >
                      >if var == 1:
                      > print 'var is one'
                      >elif var == 2:
                      > print 'var is two'
                      >[/color]
                      The trouble is that other than if/elif/else or some weird exception tricks,
                      you can't switch between statements that execute as if they were suites of if/elif/else -- i.e.,
                      in the local namespace. And UIAM current exec introduces special restrictions in a function.
                      So you can't get a constant-time "jump table" effect.

                      But IWT if we had named local code suites, we could exec them safely via a mapping. E.g.,

                      def foo(var): # following is not legal code ;-)
                      v1: print 'var is one'
                      v2: # any suite should be ok, not just one-liners
                      print 'var',
                      print 'is two'
                      switch = {1:v1, 2:v2}
                      exec switch.get(var, '')

                      It would also be nice if the local bindings of named local code suites were
                      (or became wrapped on access as) objects that were callable like functions
                      without parameters. Locally that could be optimized, but if e.g. foo returned
                      v2 to its caller, there would have to be a wrapper with a mutable closure
                      capturing the current name space IWT. This would also be interesting to
                      pass to another function, which then could effectively execute a code suite
                      in _its_ caller's space. This could lead to all kinds of wild things, _some_ of
                      which would probably be neat and useful ;-)

                      It would be nice to get the switch mapping built at def-time, which should be
                      feasible, since the v1,v2 etc, bindings could be available then. Not sure how
                      to spell it nicely though, except that I still like the notion of a general
                      way to specify def-time execution of statements or expression terms. E.g.,
                      using '..' as a syntactic marker,
                      switch ..= {1:v1, 2:v2}
                      could mean create the binding at def-time (necessarily evaluating the rhs then also).

                      I had previously proposed that
                      def foo():
                      x ..= 123
                      return x
                      be codewise equivalent to current
                      def foo(x=123):
                      return x
                      without having x appear in the argument list, but still having the same
                      call-time local-binding initialization effect (i.e., new binding to def-time rhs value).
                      I mention this because v1 and v2 in the example are not in the namespace that is normally
                      searched for default arg rhs expressions as in def foo(x=name): ..., so named local suites'
                      names would have to be included (and allowed to shadow names that might otherwise be found).

                      No one commented on my previous post on that, perhaps because I didn't split it out into
                      a separate post with appropriate title. So here I did it again ;-/ The main use would be
                      efficient local binding initialization without using the default-arg hack or using a factory
                      funtion to return a function with initialized bindings in a closure.

                      The other use would allow constant folding for terms in expressions, using ..(expr) as
                      as spelling for evaluating expr at def-time and using a constant binding to the result
                      at run-time. E.g.,

                      def circle_area(r): return ..(__import__(' math').pi)*r*r

                      would generate byte code like what you can now do with Raymond Hettinger's global-constant
                      optimizing decorator by writing:

                      pi = __import__('mat h').pi # just to make it look like the expr above
                      @bind_constants ()
                      def circle_area(r): return pi*r*r
                      del pi

                      (Very nice, Raymond)

                      Maybe Raymond will make a decorator to do local variable initialization, so we can write
                      @localinit('nam e', expr)
                      def foo():
                      name = name*2 # usable like default name in def foo(name=expr): ...
                      return name
                      ;-)

                      Regards,
                      Bengt Richter

                      Comment

                      • Leif K-Brooks

                        #12
                        Re: Is there no switch function in Python

                        Bengt Richter wrote:[color=blue]
                        > But IWT if we had named local code suites, we could exec them safely via a mapping. E.g.,
                        >
                        > def foo(var): # following is not legal code ;-)
                        > v1: print 'var is one'
                        > v2: # any suite should be ok, not just one-liners
                        > print 'var',
                        > print 'is two'
                        > switch = {1:v1, 2:v2}
                        > exec switch.get(var, '')[/color]

                        I'm probably missing something, but what's wrong with:

                        def foo(var):
                        def v1(): print 'var is one'
                        def v2():
                        print 'var',
                        print 'is two'
                        switch = {1:v1, 2:v2}
                        switch.get(var, lambda:None)()

                        Comment

                        • Leif K-Brooks

                          #13
                          Re: Is there no switch function in Python

                          Jaime Wyant wrote:[color=blue]
                          > Don't blame c++. This is a relic of 'C'.[/color]

                          Kind of like saying, "Don't blame me for being a violent thug, I went to
                          ShootEm Elementary School." Sure, C++ inherited lots of questionable
                          features from C, but there's never been a law requiring all programming
                          languages to be extensions of C. The designers of C++ must have known
                          about C's faults; it's their own fault for choosing to use it in the
                          first place.

                          Comment

                          • Alex Martelli

                            #14
                            Re: Is there no switch function in Python

                            Leif K-Brooks <eurleif@ecritt ers.biz> wrote:
                            [color=blue]
                            > Bengt Richter wrote:[color=green]
                            > > But IWT if we had named local code suites, we could exec them safely via[/color][/color]
                            a mapping. E.g.,[color=blue][color=green]
                            > >
                            > > def foo(var): # following is not legal code ;-)
                            > > v1: print 'var is one'
                            > > v2: # any suite should be ok, not just one-liners
                            > > print 'var',
                            > > print 'is two'
                            > > switch = {1:v1, 2:v2}
                            > > exec switch.get(var, '')[/color]
                            >
                            > I'm probably missing something, but what's wrong with:
                            >
                            > def foo(var):
                            > def v1(): print 'var is one'
                            > def v2():
                            > print 'var',
                            > print 'is two'
                            > switch = {1:v1, 2:v2}
                            > switch.get(var, lambda:None)()[/color]

                            Nothing wrong. However, if v1 and v2 needed to rebind some names within
                            foo, they couldn't according to def's semantics -- they could only
                            rebind local names of their own, or global names with a global
                            statement, but not local names in their outer function foo. I think the
                            right approach, if that feature is a must-have, might be to add the one
                            missing feature, not with a weird new syntax which forces one to choose
                            between taking arguments and rebinding outer functions' locals, but with
                            an optional scope indicator on variables. Just IMHO, anyway.


                            Alex

                            Comment

                            • Bengt Richter

                              #15
                              Re: Is there no switch function in Python

                              On Fri, 10 Sep 2004 22:20:13 -0400, Leif K-Brooks <eurleif@ecritt ers.biz> wrote:
                              [color=blue]
                              >Bengt Richter wrote:[color=green]
                              >> But IWT if we had named local code suites, we could exec them safely via a mapping. E.g.,
                              >>
                              >> def foo(var): # following is not legal code ;-)
                              >> v1: print 'var is one'
                              >> v2: # any suite should be ok, not just one-liners
                              >> print 'var',
                              >> print 'is two'
                              >> switch = {1:v1, 2:v2}
                              >> exec switch.get(var, '')[/color]
                              >
                              >I'm probably missing something, but what's wrong with:
                              >
                              >def foo(var):
                              > def v1(): print 'var is one'
                              > def v2():
                              > print 'var',
                              > print 'is two'
                              > switch = {1:v1, 2:v2}
                              > switch.get(var, lambda:None)()[/color]

                              Sorry, I should have shown an example that really depends on local name space rebinding,
                              which the OP's example doesn't, and which was my main point. E.g., try getting the effect of:

                              def foo(var): # following is not legal code ;-)
                              v1: print 'var is one'
                              var += 1
                              status = 'incremented'
                              v2: # any suite should be ok, not just one-liners
                              print 'var',
                              print 'is two'
                              var *= 2
                              status = 'doubled'
                              switch = {1:v1, 2:v2}
                              exec switch.get(var, '')
                              print 'var is now %r, and was %s.' % (var, status)
                              return var, status

                              Regards,
                              Bengt Richter

                              Comment

                              Working...