字典作为案例陈述

字典作为案例陈述

本文介绍了列表/字典作为案例陈述?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如果我没弄错的话,我在某处读到你可以在列表和/或词典中使用

函数名/引用,但现在我可以'$

再找到它。


想法是为某种微控制器构建一个模拟器(只需

作为一般惯例,我希望它也很慢;-)。


opcodes = {

1:(''MOV'',function1, ...),

2 :(''ADD'',function2,)

3:(''MUL'',class3.function3,)

}


def function1

#做一些复杂的事情

这可能吗?


谢谢,

Stef Mientki


If I''m not mistaken, I read somewhere that you can use
function-names/references in lists and/or dictionaries, but now I can''t
find it anymore.

The idea is to build a simulator for some kind of micro controller (just
as a general practise, I expect it too be very slow ;-).

opcodes ={
1: (''MOV'', function1, ...),
2: (''ADD'', function2, ),
3: (''MUL'', class3.function3, )
}

def function1
# do something complex
Is this possible ?

thanks,
Stef Mientki

推荐答案



是。


你正在实施的通常被称为

调度员,他们经常用字典完成

,如你所示。


-

格兰特爱德华兹格兰特哇!我猜你们在BIG MUSCLES上也得到了b
很多研究了

Yes.

What you''re implementing is commonly referred to as a
"dispatcher", and they''re often done with a dictionary exactly
as you show.

--
Grant Edwards grante Yow! I guess you guys got
at BIG MUSCLES from doing too
visi.com much STUDYING!




是的。函数是(所谓的)第一类对象。您可以按名称引用一个

,并在变量和其他数据中传递该引用

结构。


表示,你上面的代码不会像写的那样工作,因为当你引用它时,function1是不存在的。


这里有一些工作代码它操纵对函数的引用

然后调用它:

Yes. Functions are (so called) first class objects. You can refer to one
by name, and pass that reference around in variables and other data
structures.

That said, your code above won''t work as written because function1 is
not in existence when you refer to it.

Here''s some working code which manipulates a reference to a function
then calls it:



.... print" Hello world!"

....

.... print "Hello world!"
....



Hello world!

Hello world!



Hello world!

Hello world!



Hello world!

Hello world!



Hello world!

Hello world!



Hello world!

Hello world!



Gary Herron

Gary Herron




是的,我刚刚发现了。


感谢Gary和Grant,

这个原则真的很有魅力。


欢呼,

Stef Mientki

Yes, I just found that out.

Thanks Gary and Grant,
this principle really works like a charm.

cheers,
Stef Mientki


这篇关于列表/字典作为案例陈述?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 03:37