0

I need to take name of function that I want to call as parameter from a list. I achieved this with the code (the result is b. if I say liste[0] result is a, so it works.):

def a():
    print("aaaa")
    return None
def b():
    print("bbbb")
    return None
liste = ['a','b']
inputMethodName =  liste[1]
locals()[inputMethodName]()

But in this example I do same thing(at least I see in that way thats why I ask this question), it is giving an error. Code is:

filterPassFlag = 1
controlListForFilter = ['firstCharController']

def firstCharController(singleLine):
    if singleLine[0] == "1":
        filterPassFlag = 0
    return None

def startControl(singleLine):
    controlListForFilterIterator  = 0
    while (controlListForFilterIterator < len(controlListForFilter)) & (filterPassFlag == 1):
        inputMethodName = controlListForFilter[controlListForFilterIterator]
        locals()[inputMethodName](singleLine) #******ERROR IN THAT LINE.
        controlListForFilterIterator = controlListForFilterIterator + 1
    return None

singleLine = "bla bla bla bla"
startControl(singleLine)

output is:

> Traceback (most recent call last):   File
> "C:/Users/BerkayS/Desktop/phyondeneme/phytonTest.py", line 37, in
> <module>
>     startControl(singleLine)   File "C:/Users/BerkayS/Desktop/phyondeneme/phytonTest.py", line 24, in
> startControl
>     locals()[inputMethodName](singleLine) KeyError: 'firstCharController' Process finished with exit code 1

When I delete the line contains the error (local() line) program is working. But what is the difference between two example?

abidinberkay
  • 1,857
  • 4
  • 36
  • 68

0 Answers0