避免if..elsif语句 [英] Avoiding if..elsif statements

查看:114
本文介绍了避免if..elsif语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序,根据字典中的特定值,我会调用另一个函数。目前,我已经实现了一堆

if..elif语句来实现这一目标,但它现在已经超过30了

现在已经得到了相当乏味。有没有更有效的方法来做这个?b / b
代码:


value = self.dictionary。 get(keyword)[0]


if value ==" something":

somethingClass.func()

elsif value ==" somethingElse":

somethingElseClass.func()

elsif value ==" anotherthing":

anotherthingClass.func( )

elsif value ==" yetanotherthing":

yetanotherthingClass.func()


是否可以存储这些功能在字典中调用以便我可以调用字典值吗?

解决方案

" unexpected" ; < su *********** @ gmail.comwrote:


我有一个程序,根据字典中的特定值,我

调用不同的函数。目前,我已经实现了一堆

if..elif语句来实现这一目标,但它现在已经超过30了

现在已经得到了相当乏味。有没有更有效的方法来做这个?b / b
代码:


value = self.dictionary。 get(keyword)[0]


if value ==" something":

somethingClass.func()

elsif value ==" somethingElse":

somethingElseClass.func()

elsif value ==" anotherthing":

anotherthingClass.func( )

elsif value ==" yetanotherthing":

yetanotherthingClass.func()


是否可以存储这些功能在字典中调用以便我可以调用字典值吗?



但当然(你试过吗?)。这是一个大纲:


dispatch = {

" something":somethingClass.func,#note:no()here

somethingElse:somethingElseClass.func,

anotherthing:anotherthingClass.func,

yetanotherthing:yetanotherthingClass.func,
}


...


dispatch [value]()#note:在这里拨打电话!


或更强劲:


尝试:

func = dispatch [value]
$ b除了KeyError之外的$ b:

print" - 没有for的处理程序,值

else:

func()


根据需要进行调整。


< / F>


代码:


>

value = self.dictionary.get(keyword)[0]


if value ==" something":

somethingClass.func()

elsif value ==" somethingElse":

somethingElseClass.func()

elsif value ==" anotherthing":

anotherthingClass.func()

elsif value == yetanotherthing:

yetanotherthingClass.func()


是否可以将这些函数调用存储在字典中以便我

可以调用字典值吗?



如何(未经测试):


def x():

print'' x''


def y():

打印''y''


funcdict = {' 'valuex'':x,''valuey'':y}


funcdict [''valuex'']()


< blockquote> unexpected写道:


我有一个程序,根据字典中的特定值,我

调用不同的函数。目前,我已经实现了一堆

if..elif语句来实现这一目标,但它现在已经超过30了

现在已经得到了相当乏味。有没有更有效的方法来做这个?b / b
代码:


value = self.dictionary。 get(keyword)[0]


if value ==" something":

somethingClass.func()

elsif value ==" somethingElse":

somethingElseClass.func()

elsif value ==" anotherthing":

anotherthingClass.func( )

elsif value ==" yetanotherthing":

yetanotherthingClass.func()


是否可以存储这些功能在字典中调用以便我可以调用字典值吗?



是的。


dispatch = dict(

something = somethingClass.func,

somethingElse = somethingElseClass.func,

anotherthing = anotherthingClass.func,

yetanotherthing = yetanotherthingClass.func




def默认值():

传递

#这样称呼


dispatch .get(switch_value,默认)()


I have a program where based on a specific value from a dictionary, I
call a different function. Currently, I''ve implemented a bunch of
if..elsif statements to do this, but it''s gotten to be over 30 right
now and has gotten rather tedious. Is there a more efficient way to do
this?

Code:

value = self.dictionary.get(keyword)[0]

if value == "something":
somethingClass.func()
elsif value == "somethingElse":
somethingElseClass.func()
elsif value == "anotherthing":
anotherthingClass.func()
elsif value == "yetanotherthing":
yetanotherthingClass.func()

Is it possible to store these function calls in a dictionary so that I
could just call the dictionary value?

解决方案

"unexpected" <su***********@gmail.comwrote:

I have a program where based on a specific value from a dictionary, I
call a different function. Currently, I''ve implemented a bunch of
if..elsif statements to do this, but it''s gotten to be over 30 right
now and has gotten rather tedious. Is there a more efficient way to do
this?

Code:

value = self.dictionary.get(keyword)[0]

if value == "something":
somethingClass.func()
elsif value == "somethingElse":
somethingElseClass.func()
elsif value == "anotherthing":
anotherthingClass.func()
elsif value == "yetanotherthing":
yetanotherthingClass.func()

Is it possible to store these function calls in a dictionary so that I
could just call the dictionary value?

but of course (did you try it?). here''s an outline:

dispatch = {
"something": somethingClass.func, # note: no () here
"somethingElse": somethingElseClass.func,
"anotherthing": anotherthingClass.func,
"yetanotherthing": yetanotherthingClass.func,
}

...

dispatch[value]() # note: do the call here!

or, a bit more robust:

try:
func = dispatch[value]
except KeyError:
print "- no handler for", value
else:
func()

tweak as necessary.

</F>


Code:

>
value = self.dictionary.get(keyword)[0]

if value == "something":
somethingClass.func()
elsif value == "somethingElse":
somethingElseClass.func()
elsif value == "anotherthing":
anotherthingClass.func()
elsif value == "yetanotherthing":
yetanotherthingClass.func()

Is it possible to store these function calls in a dictionary so that I
could just call the dictionary value?

How about (untested):

def x():
print ''x''

def y():
print ''y''

funcdict={ ''valuex'': x, ''valuey'': y }

funcdict[''valuex'']()


unexpected wrote:

I have a program where based on a specific value from a dictionary, I
call a different function. Currently, I''ve implemented a bunch of
if..elsif statements to do this, but it''s gotten to be over 30 right
now and has gotten rather tedious. Is there a more efficient way to do
this?

Code:

value = self.dictionary.get(keyword)[0]

if value == "something":
somethingClass.func()
elsif value == "somethingElse":
somethingElseClass.func()
elsif value == "anotherthing":
anotherthingClass.func()
elsif value == "yetanotherthing":
yetanotherthingClass.func()

Is it possible to store these function calls in a dictionary so that I
could just call the dictionary value?

Yup.

dispatch = dict(
something = somethingClass.func,
somethingElse = somethingElseClass.func,
anotherthing = anotherthingClass.func,
yetanotherthing = yetanotherthingClass.func
)

def default():
pass
# call it like this

dispatch.get(switch_value, default)()


这篇关于避免if..elsif语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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