另一个具有功能的开关盒
深入研究有趣的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当你在做这样的事情时:
实际上你正在评估(调用)函数
readt3
,参数e,t,off,partElems,partNodes
我认为你想要的do 是这样的(模拟 switch case 语句):现在你可以在给定
case
参数的情况下调用你的 case,如下所示:when you are doing something like this:
actually you are evaluating (calling) the function
readt3
with the argumentse,t,off, partElems, partsNodes
what i think you want to do is something like this (to emulate switch case statement ) :and now you can call your cases given a
case
argument like this :实际上,使用字典来调度呼叫有时是可以做到的,只要有意义,并且当您知道自己在做什么时。
在其他语言中代替“
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.
字典并不意味着用作构造。它不是用来存储结构而是真实的数据。在你的情况下,我认为最好定义一个对象。考虑在这种情况下使用面向对象。
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.
在这里,您使用未定义的参数 e、t、off 等调用函数 readt3。
Here you are calling the function readt3 with arguments e,t,off,etc., that are not defined.