[Tutor] switch statements (original) (raw)
Gregor Lingl glingl at aon.at
Fri Jul 9 11:23:11 CEST 2004
- Previous message: [Tutor] switch statements
- Next message: [Tutor] switch statements
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Karthikesh Raju schrieb:
Hi All,
We were implementing a switch satetement like testfile.py dec = 1 _perf = { _ _1:bigfile.func1(), _ 2:bigfile.fun2()}[dec] now bigfile.py has def func1(): print "In func1" return 5 def func2(): print "In func2" return 42 Now when we run the testfile.py we get: In func1 In func2
Certainly! You will get a clueon what's going on, if you delete [dec] and have a look at perf: def func1(): print "In func1" return 5
def func2(): print "In func2" return 42
dec = 1
perf = {
1:func1(),
2:func2()}
will output:
In func1 In func2
perf {1: 5, 2: 42}
Clearly perf[1] outputs 5.
What you intended was, that your perf dictionary contains function object. Then you must not evaluate the functions (which is done, if you attach a pair of parentheses) :
dec = 1
perf = {
1:func1,
2:func2}
Look at perf:
perf {1: <function func1 at 0x00A4F2B0>, 2: <function func2 at 0x00A4F270>}
So perf contains two functions. The first one is:
perf[1] <function func1 at 0x00A4F2B0>
You can call it
perf1 In func1 5
To summarize, your program will do what you expected - if I understand you right - , if you change it that way:
testfile.py
dec = 1
perf = {
1:bigfile.func1,
2:bigfile.fun2}dec
Regards, Gregor
- Previous message: [Tutor] switch statements
- Next message: [Tutor] switch statements
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]