Ask coding questions

← Back to all posts
Python functions question?
isaiah08 (86)

If i have a variable that contains a function name, and i wanted to run that function in the variable, how would i do it?

(Without just doing funcname().)

Answered by Pankak (49) [earned 5 cycles]
View Answer
Comments
hotnewtop
isaiah08 (86)

I would be more inclined to believe you if your profile didn't say 'I don't know things.' @Pankak

Pankak (49)

@isaiah08 if you stored the names of the functions inside a list/array or object (depends on what language) and referenced the name to the function, then it could be possible

isaiah08 (86)

How? I'm using python. @Pankak

Bookie0 (6358)

Hi,

Wdym "have a variable that contains a function name"?
do you mean like this:

def hello(): # the function
  print("hello")

a_variable = hello() # a function in a variable

In this case, I don't think its possible, but I think you can store functions inside arrays tho...

isaiah08 (86)

i mean
x = 'func()'
run(x)

@Bookie0

Bookie0 (6358)

@isaiah08 hum ok have you tried that code in an editor? Ive never heard of the run keyword

isaiah08 (86)

neither have i, it was just an example. @Bookie0

Bookie0 (6358)

sry, idk, try googling it maybe.. @isaiah08