Another junction box with functions

Diving is deeper in an interesting python language, so there is no switch in the language as a construct. Thus, using dictionaries is the first choice of location after reading the first edition of python. So I tried how,

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[partPnt].append(e)
    nods = t[offset: offset+3];
    for n in nods:
      partsNodes[partPnt].append(n)
    return 

And I got the error "readt3 is undefined", I thought I got it because it is not defined before the case, and then moved the function definitions above the cases, but still the same problem, but this time "e is not defined" I do not could understand this, so e is a parameter of the function, why am I getting a definition error related to e?

Where should the function definitions be given when emulating the switching case in this situation?

+3
source share
4

- :

...
 2 : readt3(e,t,off, partElems, partsNodes)
...

() readt3 e,t,off, partElems, partsNodes, , , - ( case switch):

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
        }   

case :

case = 2

cases[case](e,t,off, partElems, partsNodes)
+9

, - , , , , .

Python, "switch...case" , "if..elif..else".

, . , " " - . .., , , if-else: ( C, , ), if-elif .

, case switch C, ( ). ... , , , , 10 20 - , , .

, " " , , if-elif-else.

+2

. , . , . OO .

+1
cases = { 2 : readt3(e,t,off, partElems, partsNodes), 

readt3 e, t, off .., .

0

Source: https://habr.com/ru/post/1785222/


All Articles