I have a list of class names as strings, and if I say fe. print(cNames[0])
it will result into Foo
since cNames[0] = "Foo"
. Now if I wanted to compare a class string with a class, I would do this:
if eval(cNames[0]) in classes:
foo = eval(cNames[0])()
How ever, it's giving me invalid syntax error:
File ".\testing.py", line 54, in convert
print("Class: %s" %eval(cNames[0]))
File "<string>", line 1
<class 'testing.Foo'>
^
SyntaxError: invalid syntax
And I know I'm only printing the class name, but it didn't work for if sentence either, so I tried to do it with a print. No effect
EDIT:
So the problem appeared to be me calling eval("<class 'testing.Foo'>")
instead of eval("Foo")
. Now this leads into few problems, I'm working with sqlite3 database and I'm trying to save a string as following: <class>:i
for example Foo:53
, but instead I'm doing <class 'testing.Foo'>:53
, and that's why calling eval on it wouldn't work. How could I turn <class 'testing.Foo'>
(not a string, the class itself) into string "Foo"?