pythonexecpython-3.6standards-compliance

how can I execute a string formatting with out using exec function in python


I have an exec statement to set the formatting of two variables in python 3.6. Even though the code works fine, I need to change this due to compliance issue. Please let me know how it can be done differently.

My statement: exec("{}='{}'".format(item,s))

thanks in advance for your help.


Solution

  • Without knowing more:

    locals()[item] = str(s)
    

    will work for "simple" names (foo, x, etc.), but not for more complex assignments that your exec approach could handle (e.g. instance.attr)

    In either case, "there must be a better way" to do what you're trying to do, but there's no context to help with that.