列表/字典作为案例陈述? [英] list/dictionary as case statement ?

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

问题描述



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

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

再找到它。


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

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


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

推荐答案

2007-01-02,Stef Mientki< ; S. ************** @ mailbox.kun.nlwrote:
On 2007-01-02, Stef Mientki <S.**************@mailbox.kun.nlwrote:

如果我没弄错,我读了在某个地方你可以使用

列表和/或词典中的函数名/引用,但

现在我再也找不到它了。


想法是为某种微型

控制器建立一个模拟器(就像一般练习,我希望它也是非常慢的; /)。


opcodes = {

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

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

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

}


def function1

#做一些复杂的事情


这可能吗 ?
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 ?



是。


你正在实施的通常被称为

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

,如你所示。


-

格兰特爱德华兹格兰特哇!我猜你们在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!


Stef Mientki写道:
Stef Mientki wrote:

如果我没有弄错,我会在某处读到你可以使用

列表中的函数名/引用和/或者词典,但是现在我不能再次找到它了。


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

作为一般做法,我预计它也会很慢;-)。


opcodes = {

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

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

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

}


def function1

#做一些复杂的事情

$ b这是可能的吗?


谢谢,

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



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

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

结构。


表示,你上面的代码不会像写的那样工作,因为当你引用它时,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:


>> def fn():
>>def fn():



.... print" Hello world!"

....

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


>> x = fn
y = [fn,fn]
z = {1:fn,2:fn}

x()
>>x = fn
y = [fn,fn]
z = {1:fn, 2:fn}

x()



Hello world!

Hello world!


>> y [0]()
>>y[0]()



Hello world!

Hello world!


>> y [1]()
>>y[1]()



Hello world!

Hello world!


>> z [1]()
>>z[1]()



Hello world!

Hello world!


>> z [2]()
>>z[2]()



Hello world!

Hello world!


>>>
>>>



Gary Herron

Gary Herron


>
>

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

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

结构。


表示,上面的代码不会像写的那样工作,因为当你引用它时,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.



是的,我刚刚发现了。


感谢Gary和Grant,

这个原则真的很有魅力。


欢呼,

Stef Mientki

Yes, I just found that out.

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

cheers,
Stef Mientki


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆