另一个具有功能的开关盒

发布于 2024-10-11 12:58:07 字数 761 浏览 3 评论 0原文

深入研究有趣的 python 语言,因此该语言作为构造没有任何切换。所以使用字典是阅读学习Python第一版的首选。所以我尝试了类似的方法,

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 

并收到错误“readt3未定义”,我想我得到这个是因为它在案例之前没有定义,然后将函数定义移到案例上方,但仍然是同样的问题,但这次“e未定义” “我无法理解这一点,所以 e 是一个函数参数,为什么我会在 e 上收到与定义相关的错误?

在这种情况下模拟 switch-case 时,函数定义应该放在哪里?

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[partPnt].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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

那请放手 2024-10-18 12:58:07

当你在做这样的事情时:

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

实际上你正在评估(调用)函数readt3,参数e,t,off,partElems,partNodes我认为你想要的do 是这样的(模拟 switch case 语句):

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,如下所示:

case = 2

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

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)
韶华倾负 2024-10-18 12:58:07

实际上,使用字典来调度呼叫有时是可以做到的,只要有意义,并且当您知道自己在做什么时。

在其他语言中代替“switch...case”的 Python 语法结构是“if..elif..else”。

我不知道为什么人们就是不这么做。这就像一个人会“失去表现”或其他什么。但是..switch case,当人们认为时,是 if-else 链的一种极其狭窄的特殊情况:它只允许比较相等性,并且只允许比较整数(至少在 C 中,我不知道每个克隆 C 语法的语言),而在 if-elif 链中可以使用任何表达式。

很容易看出 Switch case 语句刚刚在 C 中引入,因为它允许编译器自动针对这种特殊情况进行优化(使用跳转表,而不是一系列比较)。但是......这仅对编译语言有意义,然后,只有当编译器无法单独处理这些优化时才有意义,即使如此,也只有在 10 或 20 次比较的速度差异是这样时才有意义比使用跳转表更大。

正如您所看到的,在现代高级语言中使用“switch case”是相当多余的,这是因为可以链接 if-elif-else 语句。

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.

双手揣兜 2024-10-18 12:58:07

字典并不意味着用作构造。它不是用来存储结构而是真实的数据。在你的情况下,我认为最好定义一个对象。考虑在这种情况下使用面向对象。

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.

新雨望断虹 2024-10-18 12:58:07
cases = { 2 : readt3(e,t,off, partElems, partsNodes), 

在这里,您使用未定义的参数 e、t、off 等调用函数 readt3。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文