PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, July 22, 2022

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

 July 22, 2022     exec, python, python-3.6, standards-compliance     No comments   

Issue

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.



Answered By - jedwards
Answer Checked By - David Goodson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing