[Python-ideas] string.format() default variable assignment

Dustin J. Mitchell dustin at v.igoro.us
Fri Mar 1 18:08:56 CET 2013


On Fri, Mar 1, 2013 at 11:55 AM, 김용빈 <kybinz at gmail.com> wrote:
> why we bother with '{variable}'.format(variable=variable) ?
> can we just '{variable}.format()' ?
>
> if variable is exist, then assign it.
> if variable is not exist, then raise error
>
> I am not language expert. so sorry if this is not a good idea, or already
> discussed.

Explicit is better than implicit.

There are also security issues with automatically making all local
variables available to a format string, where that format string might
come from an untrusted source.

If you want the behavior you suggest, you can use
  '{variable}'.format(**locals())
unless you don't intend to include global variables.  I don't recall
the behavior of locals() with regard to non-global variables from
enclosing scopes.

Dustin



More information about the Python-ideas mailing list