I use configparser
to get values for my python script, but I need to call function stored in config.ini
... Since all values in configparser are stored as strings, is it possible to transform string into syntax? If so, how?
Asked
Active
Viewed 57 times
0

Mirac7
- 1,566
- 4
- 26
- 44
-
You can `exec` the string, or I suppose `eval` if it is a `lambda` expression ... – mgilson May 19 '13 at 18:12
1 Answers
1
You can try using eval
, documentation here.
Or you can try exec
, documentation here.
By be careful, these are just quick and dirty way.
A better and much more safer method is to use getattr()
(documentation) and setattr()
(documentation) which indexes into globals
.
-
@Mirac7: `ast.literal_eval` is the safe way. It won't handle actual Python expressions like `'a' + 'b'`, but it will transform literal strings like `[1, 2]` and `{'a': 'b'}` into Python objects. `eval()` is easily exploitable. – Blender May 19 '13 at 18:23