英文:
Call a Python function name from concantenated string name
问题
Sure, here's the translated version:
我想创建一个名为def的函数名称,该名称是通过连接“string”+变量+“string”而生成的,并调用该def函数。
我目前正在使用这个简化的代码版本来类似地完成任务,我希望将函数do_update(a)的硬编码内容最小化:
ROTATE = '90'
ROT20 = [
[0, 0, 0, 0, 0, 0, 0, 0],
[126, 129, 153, 189, 129, 165, 129, 126],
[126, 255, 231, 195, 255, 219, 255, 126],
[0, 8, 28, 62, 127, 127, 127, 54],
[0, 8, 28, 62, 127, 62, 28, 8],
[62, 28, 62, 127, 127, 28, 62, 28],
[62, 28, 62, 127, 62, 28, 8, 8],
[0, 0, 24, 60, 60, 24, 0, 0],
];
def updatevalues90(a):
b = []
for i in range(8):
for j in range(8):
b[i] += a[j] + i
return b
def do_update(a):
if ROTATE == '90':
ROT = [updatevalues90(char) for char in a]
elif ROTATE == '180':
ROT = [updatevalues180(char) for char in a]
elif ROTATE == '270':
ROT = [updatevalues270(char) for char in a]
do_update(ROT20)
我尝试过的一切都导致无效的语法或者ROT填充了我想要的字符串名称。
我想将函数调用updatevalues90(char),而不是需要硬编码,我想将其更改为:
`ROT = ["updatevalues" + ROTATE + "(char)" for char in a]`
这样,无论ROTATE中的值是什么,它都将成为函数调用的一部分,即函数名称。
我的问题是,在Python中,我如何将字符串和变量名连接成可用的函数名称?
我认为可以使用eval,但我无法让语法为我工作。也许在Python中有一些更简单的方法可以实现?
Please note that using eval
can introduce security risks if not used carefully, and it's generally recommended to avoid using it when possible. In this case, using a dictionary or a mapping of function names to actual functions might be a safer approach than using eval
.
英文:
I want to create a def function name from concatenating "string" + variable + "string" and call that def function.
I am currently using this condensed version of code for simplicity to similarly accomplish tasks and I want to minimize the hard code contents of the function do_update(a):
ROTATE = '90'
ROT20 = [
[0, 0, 0, 0, 0, 0, 0, 0],
[126, 129, 153, 189, 129, 165, 129, 126],
[126, 255, 231, 195, 255, 219, 255, 126],
[0, 8, 28, 62, 127, 127, 127, 54],
[0, 8, 28, 62, 127, 62, 28, 8],
[62, 28, 62, 127, 127, 28, 62, 28],
[62, 28, 62, 127, 62, 28, 8, 8],
[0, 0, 24, 60, 60, 24, 0, 0],
];
def updatevalues90(a):
b = []
for i in range(8):
for j in range(8):
b[i] += a[j] + i
return b
def do_update(a):
if ROTATE == '90':
ROT = [updatevalues90(char) for char in a]
elif ROTATE == '180':
ROT = [updatevalues180(char) for char in a]
elif ROTATE == '270':
ROT = [updatevalues270(char) for char in a]
do_update(ROT20)
Everything I have tried has resulted in Invalid Syntax or ROT filled with the string name of what I want.
I want to take the function call to updatevalues90(char) and instead of needing it hard coded, I want to change it to:
ROT = ["updatevalues" + ROTATE + "(char)" for char in a]
So that whatever value is in ROTATE will become part of the function call, i.e. function name.
My question is how in Python do I concatenate the strings and a variable name into a useable function name?
I think eval, but I can't get the syntax to work for me. Maybe there is something simpler in Python that works?
答案1
得分: 5
将你的函数存储在一个dict
中:
updaters = {
'90': updatevalues90,
'180': updatevalues180,
'270': updatevalues270
}
定义一个更新函数:
def do_update(a):
ROT = [updaters[ROTATE](char) for char in a]
# 返回 ROT ?
英文:
Store your functions in a dict
:
updaters = {
'90': updatevalues90,
'180': updatevalues180,
'270': updatevalues270
}
def do_update(a):
ROT = [updaters[ROTATE](char) for char in a]
# return ROT ?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论