从合并的字符串名称中调用Python函数名称

huangapple go评论64阅读模式
英文:

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   ?

huangapple
  • 本文由 发表于 2020年1月6日 23:38:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/59614909.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定