0

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?

1
  • You can exec the string, or I suppose eval if it is a lambda expression ... Commented May 19, 2013 at 18:12

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.