I have a piece of code that looks like this. The real code is of course much longer. I have shortened it to condense it to the idea.
#!/usr/bin/python
from sys import argv
class Base():
def whoami(self):
print(self.__class__.__name__)
def foo(self, a, b):
print(a+b)
class A(Base):
def bar(self, a):
print(a)
class B(Base):
def fizz(self, a, b):
print(a+b)
def main():
eval(argv[1] + '().' + argv[2] + '(' + ', '.join(argv[3:]) + ')')
if __name__ == "__main__":
main()
Expected output:
$ ./myScript.py A whoami
A
$ ./myScript.py B fizz 5 7
12
$ ./myScript.py B fizz '5' '7'
57
But the actual output for the last line is:
$ ./myScript.py B fizz '5' '7'
12
The quotes does not get passed. My goal is to be able to call the various class methods easily from command line for debugging. I don't know if my approach is wrong from the beginning.
And yes, I'm aware of security risks with eval
. I intend to remove it when the code goes into production.