Is there no switch function in Python

Marius Bernklev mariube+netnews+ at ifi.uio.no
Thu Sep 9 16:18:37 EDT 2004


"Rudi Hansen" <rsh_remove_this_ at pobox.dk> writes:

> 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?? 

one way is using dicts:

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


-- 
Marius Bernklev



More information about the Python-list mailing list