通过文本变量调用函数 [英] call to function by text variable

查看:62
本文介绍了通过文本变量调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是的,这个主题真的没有意义吗?


无论如何我想要做的是这个:

如果n == 1 :


self.operations.insert(pos,operations.Replace.Panel(self,main))


elif n == 2:


self.operations.insert(pos,operations.ChangeCase.Panel(self,

main))


elif n == 3:


self.operations.insert(pos,operations.Move.Panel(self,main))


正如你所看到的,所有不同的函数都有相同的变量,所以

如果我可以创建一个列表并使用它会更容易。


like这个:

list = [" Replace"," ChangeCase"," Move"]

textVariable = list [n]

self.operations.insert(pos,operations。[textVariable] .Panel(self,

main))


有点像这样吗?

TIA

解决方案

ia naré写道:


像这样:


list = [" Replace"," ChangeCase"," Move" ;]

textVariable = list [n]

self.operations.insert(pos,operations。[textVariable] .Panel(self,

主))


是否有可能?



是:


self.operations.insert(

pos,

getattr(operations,textVariable).Panel(self.main)



ianaré写道:


是的,这个主题真的没有意义吗?


无论如何我想要做的就是这样:

如果n == 1:


self.operations.insert(pos,operations.Replace.Panel(self,main))


elif n == 2:


self.operations.insert(pos,operations.ChangeCase.Panel(self,

main))


elif n == 3:


self.operations.insert(pos,operations.Move.Panel(self,main))


正如你所看到的,所有不同的函数都有相同的变量,所以

如果我只能制作一个列表并使用它会更容易。


是这样的:


list = [" Replace"," ChangeCase"," Move&qu ot;]

textVariable = list [n]

self.operations.insert(pos,operations。[textVariable] .Panel(self,

main))


有可能这样吗?



的确如此。你不需要使用文字名称(尽管如此,你可以

调查getattr()) - 以下,当然是未经测试的:


ops = [operations.Replace,

operations.ChangeCase,

operations.Move

]

self .operations.insert(pos,ops [n-1] .Panel(self,main)


问候

Steve

-

Steve Holden +44 150 684 7255 +1 800 494 3119

Holden Web LLC / Ltd http://www.holdenweb.com

Skype:holdenweb http://del.icio.us/steve.holden

最近的Ramblings http://holdenweb.blogspot.com


ianaré写道:


是的,主题并不真实它有意义吗?


无论如何我想要做的就是这个:

如果n == 1:


self.operations.insert(pos,operations.Replace.Panel(self,main))


elif n == 2:


self.operations.insert(pos,operations.ChangeCase.Panel(self,

main))


elif n == 3:


self.operations.insert(pos,operations.Move.Panel(self,main))


你可以看到所有不同的功能都有相同的变量,所以

如果我可以制作一个列表并使用它会更容易。


像这样:


list = [" Replace"," ChangeCase"," Move"]

textVariable = list [n]

self.operations.insert( pos,operations。[textVariable] .Panel(self,

main))


有类似的可能吗?


的确如此。你不需要使用文字名称(尽管如此,你可以

调查getattr()) - 以下,当然是未经测试的:


ops = [operations.Replace,

operations.ChangeCase,

operations.Move

]

self .operations.insert(pos,ops [n-1] .Panel(self,main)


问候

Steve

-

Steve Holden +44 150 684 7255 +1 800 494 3119

Holden Web LLC / Ltd http://www.holdenweb.com

Skype:holdenweb http://del.icio.us/steve.holden

最近的Ramblings http://holdenweb.blogspot.com


yeah the subject doesn''t really make sense does it?

anyway want I want to do is this:
if n == 1:

self.operations.insert(pos, operations.Replace.Panel(self, main))

elif n == 2:

self.operations.insert(pos, operations.ChangeCase.Panel(self,
main))

elif n == 3:

self.operations.insert(pos, operations.Move.Panel(self, main))

As you can see all the different functions have the same variables, so
it would be easier if I could just make a list and use that.

like this:
list = ["Replace", "ChangeCase", "Move"]
textVariable = list[n]
self.operations.insert(pos, operations.[textVariable].Panel(self,
main))

Is something sort of like that possible?
TIA

解决方案

ianaré wrote:

like this:
list = ["Replace", "ChangeCase", "Move"]
textVariable = list[n]
self.operations.insert(pos, operations.[textVariable].Panel(self,
main))

Is something sort of like that possible?

Yes:

self.operations.insert(
pos,
getattr(operations, textVariable).Panel(self.main)
)


ianaré wrote:

yeah the subject doesn''t really make sense does it?

anyway want I want to do is this:
if n == 1:

self.operations.insert(pos, operations.Replace.Panel(self, main))

elif n == 2:

self.operations.insert(pos, operations.ChangeCase.Panel(self,
main))

elif n == 3:

self.operations.insert(pos, operations.Move.Panel(self, main))

As you can see all the different functions have the same variables, so
it would be easier if I could just make a list and use that.

like this:
list = ["Replace", "ChangeCase", "Move"]
textVariable = list[n]
self.operations.insert(pos, operations.[textVariable].Panel(self,
main))

Is something sort of like that possible?

Indeed. You don''t need to use textual names (though for that you can
investigate "getattr()) - the following, naturally, is untested:

ops = [operations.Replace,
operations.ChangeCase,
operations.Move
]
self.operations.insert(pos, ops[n-1].Panel(self, main)

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Recent Ramblings http://holdenweb.blogspot.com


ianaré wrote:

yeah the subject doesn''t really make sense does it?

anyway want I want to do is this:
if n == 1:

self.operations.insert(pos, operations.Replace.Panel(self, main))

elif n == 2:

self.operations.insert(pos, operations.ChangeCase.Panel(self,
main))

elif n == 3:

self.operations.insert(pos, operations.Move.Panel(self, main))

As you can see all the different functions have the same variables, so
it would be easier if I could just make a list and use that.

like this:
list = ["Replace", "ChangeCase", "Move"]
textVariable = list[n]
self.operations.insert(pos, operations.[textVariable].Panel(self,
main))

Is something sort of like that possible?

Indeed. You don''t need to use textual names (though for that you can
investigate "getattr()) - the following, naturally, is untested:

ops = [operations.Replace,
operations.ChangeCase,
operations.Move
]
self.operations.insert(pos, ops[n-1].Panel(self, main)

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Recent Ramblings http://holdenweb.blogspot.com


这篇关于通过文本变量调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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