[Tutor] switch statements

Alan Gauld alan.gauld at blueyonder.co.uk
Fri Jul 9 21:52:28 CEST 2004


> We were implementing a switch satetement like
>
> testfile.py
> dec = 1
> perf = { \
> 1:bigfile.func1(), \
> 2:bigfile.fun2()}[dec]

What this does is create a dictionary with keys 1 and 2 and values
the *results* of the two function calls. You need to lose the
parentheses to store references to the functions themselves.

> Now when we run the testfile.py we get:
>
> In func1
> In func2
>
> which implies that the switch does not actually switch, as it goes
through
> both the functions and retains the one that we asked for. Am I
missing
> something here, why should it step into every function.

What you are seeing is the functions being called as the dictionary
is defined. The actuall call to the dictionary(your switch) returns
a number value which is not seen because you don't print it.

HTH<

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld



More information about the Tutor mailing list