1

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?

asked Jan 6, 2020 at 15:38
4
  • 2
    Have you considered the possible security implications of this approach? There's a good chance you don't need this kind of flexibility at all. You might just need function overloading or an additional parameter to your function. Commented Jan 6, 2020 at 15:41
  • "and a variable name" - it's not possible (or, possible, but unnecessarily difficult to do) to obtain the name of a variable used in an expression Commented Jan 6, 2020 at 15:42
  • Ideally, you can define a single function that takes the angle of rotation as an additional parameter. I suspect the various updatevalues* functions look very similar. Commented Jan 6, 2020 at 15:43
  • While probably not a good idea, what you actually are asking to do can be done with eval() and similar approaches. Commented Jan 6, 2020 at 15:44

1 Answer 1

5

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 ?
answered Jan 6, 2020 at 15:41
Sign up to request clarification or add additional context in comments.

3 Comments

This looks sweet and what I'm looking for but I get this error and don't know why. ROT = [updaters['ROTATE'](char) for char in a] KeyError: 'ROTATE'
Dumb mistake by me; I quoted the key instead of using the variable. I'll fix.
That was the first thing I checked and it errored on me after I fixed it....because I accidentally renamed a variable.... and didn't take a moment to go find it... So I went and found it. But hey, it's working like a champ! Thanks for that.... that's a sweet and simple approach and fix.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.