Is there no switch function in Python

Peter Otten __peter__ at web.de
Fri Sep 10 17:24:25 CEST 2004


Isaac To wrote:

> 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);
>     }
>   }

This is otherwise known a Duff's device:

http://catb.org/~esr/jargon/html/D/Duffs-device.html

Peter




More information about the Python-list mailing list