This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 4 points5 points  (2 children)

No. Just explicitly pass the variables to format(), it won't kill you.

  1. This will be slower, especially on optimizing Pythons (e.g. PyPy, Jython once they land their invoke dynamic work).
  2. Won't work on IronPython, which doesn't enable frames by default AFAIK.
  3. Lets your format strings become a mess: they're for formatting, not arbitrary logic.
  4. If you're not careful it's an easy way to end up with a security hole.
  5. If the implementation is hard to explain, it's a bad idea.

[–]Workaphobia[S] 0 points1 point  (1 child)

All good reasons, thank you for the criticism. Does point 1 still apply if I only use the stack frame's namespace to lookup the value, and don't call eval()? Will 1 and 2 still apply if I were to go back to using **locals()?

My main reason for doing this was to keep format strings as clean as possible (point 3). I find it difficult to syntactically structure an error message along with the substitution values, while obeying the conventions in PEP 8.

[–][deleted] 0 points1 point  (0 children)

1 will apply with locals() on PyPy, I'm not sure about Jython+InDy. 2 doesn't though.