-1

A custom_string input given is alphanumeric input.
If i from the for loop happens to be a number in any iteration, then one among the custom functions that is written inside another python file letterMatrix as def num_0: (....) def num_1: (....) def num_2: (....) is called based on the value of the variable i

I cannot call that custom function (say) num_0 (when i == 0) like how I have, in the last line.
How can I call that letterMatrix.num_1() (say i == 1) based on the value in variable i

import letterMatrix
others = {0 : 'num_0', 1 : 'num_1', 2 : 'num_2', 3 : 'num_3', 4 : 'num_4', 5 : 'num_5', 6 : 'num_6', 7 : 'num_7', 8 : 'num_8', 9 : 'num_9'}

         for i in custom_string:
             if i.isnumeric():
                others[letterMatrix.i](self.font_size, self.character, row)
Shashank Raj
  • 153
  • 2
  • 12
  • Functions are first-class citizens in Python. You can use the function instead of the function name as the value in the dictionary. Then, `others[i](...)` will call the function. – Pranav Hosangadi Feb 02 '21 at 15:20
  • But having put ```others[i](self.font_size, self.character, row)``` in the last line, will give this error ```KeyError: '0'``` – Shashank Raj Feb 02 '21 at 15:29
  • That's because the key `'0'` (a string) doesn't exist in your dict. Convert `i` to an `int`, or change the keys of your dict to strings. – Pranav Hosangadi Feb 02 '21 at 15:30
  • But again, that is giving me an error saying - ```TypeError: 'str' object is not callable``` Can you rewrite my code from the question to make it work, please? Thnks – Shashank Raj Feb 04 '21 at 04:28
  • _Because strings are not callable, functions are!_ Do what I suggested in the first comment: Instead of having a string containing the function name in the dictionary, use the function itself. Alternatively, google ["calling a function of a module using its name from a string"](https://www.google.com/search?q=calling+a+function+module+using+its+name+from+a+string) – Pranav Hosangadi Feb 04 '21 at 15:56
  • Does this answer your question? [Calling a function of a module by using its name (a string)](https://stackoverflow.com/questions/3061/calling-a-function-of-a-module-by-using-its-name-a-string) – Pranav Hosangadi Feb 04 '21 at 15:56
  • I wanted a dictionary way of solving this than using ```eval``` or ```getattr```. and using ```others[i]()``` as per your first comment --
    I see this ```TypeError: 'str' object is not callable``` when ```others = {'0' : 'num_0'}``` and ```NameError: name 'num_0' is not defined``` with ```others = {0 : num_0}```
    – Shashank Raj Feb 04 '21 at 16:27
  • Re. NameError: because `num_0` _is not defined!_ On the other hand, I suspect `letterMatrix.num_0` is perfectly well defined. Programming isn't a trial-and-error job where you throw things at the wall and see what sticks. _Think_ about your code and what it's asking the interpreter to do. _Read_ the error messages, they contain important hints about how to fix the error. Step through and debug your code to see where its behavior differs from what you expect. [How to debug small programs.](//ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Pranav Hosangadi Feb 04 '21 at 16:30

1 Answers1

1

Use getattr() instead.

others = {0 : 'num_0', 1 : 'num_1', 2 : 'num_2', 3 : 'num_3', 4 : 'num_4', 5 : 'num_5', 6 : 'num_6', 7 : 'num_7', 8 : 'num_8', 9 : 'num_9'}

#custom_string = 'a0b1c2'  #Example

for i in custom_string:
    if i.isnumeric():
        getattr(letterMatrix, others[i])(self.font_size, self.character, row)
Shashank Raj
  • 153
  • 2
  • 12