Diving deeper in the interesting python language, so there is no switch in the language as a construct. So using dictionaries is the first place choice by reading learning python first edition. So I tried sth like,
cases = { 2 : readt3(e,t,off, partElems, partsNodes), # to read the triangular elements
3 : readq4(e,t,off, partElems, partsNodes), # to read the quadrangular elements
5 : readh8(e,t,off, partElems, partsNodes), # to read the hexa elements
}
# define functions
def readt3( e, t, off, partElems, partsNodes, partPnt ):
partsElems[partP开发者_StackOverflow社区nt].append(e)
nods = t[offset: offset+3];
for n in nods:
partsNodes[partPnt].append(n)
return
And got the error "readt3 is undefined", I thought I got this because it is not defined before the case then moved the function definitions up above cases but still the same problem but this time "e is not defined" I could not understand this, so e is a function parameter, why do I get a definition related error on e?
Where should the functions definitions be placed while emulating a switch-case in this situation?
when you are doing something like this:
...
2 : readt3(e,t,off, partElems, partsNodes)
...
actually you are evaluating (calling) the function readt3
with the arguments e,t,off, partElems, partsNodes
what i think you want to do is something like this (to emulate switch case statement ) :
def readt3( e, t, off, partElems, partsNodes, partPnt ):
partsElems[partPnt].append(e)
nods = t[offset: offset+3];
for n in nods:
partsNodes[partPnt].append(n)
return
# And of course all your function definition should be here before the cases dict.
cases = { 2 : readt3, # to read the triangular elements
3 : readq4, # to read the quadrangular elements
5 : readh8, # to read the hexa elements
}
and now you can call your cases given a case
argument like this :
case = 2
cases[case](e,t,off, partElems, partsNodes)
Actually, the use of dictionaies to dispatch calls is a thing that can be done sometimes, where it makes sense, and when you kno what you are doing.
The Python syntax construct that is used in place of "switch...case
" in other languages is "if..elif..else
".
I don't know why people simply don't go for it. It is like one will be "loosing performance" or whatever. But..switch case, whnen one thinks, is an extremely narrowed down special case of an if--else chain: it just allows comparison for equality, and only comparison of integers (in C at least, I don't know about every language out there which clones C syntax), while in an if-elif chain one can use any expression.
It is easy to perceive that the Switch case statement was just introduced in C because it allowed the compiler to automate optimizations for this special case (with jump tables, instead of a sequence of comparisons). But...that only makes sense for compiled languages, and then, it only makes sense when the compiler could not take care of these optimizations alone, and even then, it only makes sense if the sped difference for 10 or 20 compares is that greater than using a jump table.
As you can see, it is quite redundant to have a "switch case" in a modern high level language, and that is because one can chain if-elif-else statements.
The dictionary is not meant to be used as a construct. It is not used to store the structure but the real data. In you case I think it is better to define a object. Think about using OO in this scenario.
cases = { 2 : readt3(e,t,off, partElems, partsNodes),
Here you are calling the function readt3 with arguments e,t,off,etc., that are not defined.
精彩评论