0

Let's say I have a value callfunc in list list1 that I want to call on the next value in the list, but only if callfunc is in list2 (it is). I have this code:

for instr in list1:
if instr in list2:
    eval(instr(list1[list1.index(instr)+1]))

And I get this error message:

eval(instr(list1[list1.index(instr)+1]))
TypeError: 'str' object is not callable

I'm trying to get the output as a function, not a string of:

callfunc(75)

That will, in the script, call callfunc on 75.

lists 1 and 2:

list1 = [callfunc, 75]
list2 = [callfunc]

I have no idea where to go from here. Help would be greatly appreciated.

callfunc is a string.

Gavyn Rogers
  • 37
  • 1
  • 8

2 Answers2

1

I don't really understand why you use eval here. If you want to call the function, just do this:

for ifunc in list1:
if ifunc in list2:
    d = ifunc(list1[list1.index(ifunc)+1])
    print(d)
ljk321
  • 16,242
  • 7
  • 48
  • 60
0

Try:

eval(instr+"("+list1[list1.index(instr)+1]+")")

This will create the call string you wanted.

Then read this question and it's answers.

Community
  • 1
  • 1
Reut Sharabani
  • 30,449
  • 6
  • 70
  • 88